Intel+Ardruino 101

为了传说中的那啥, 啊, 嗯..

#include <CurieBLE.h>

const int ledPin = 13; // set ledPin to on-board LED  LED的pin脚就是14
const int buttonPin = 4; // set buttonPin to digital pin 4   按键pin脚是4

BLEPeripheral blePeripheral; // create peripheral instance    这里就好像一个起一个对象一样, 连new都不用, 就特么起了一个外设, 牛逼, java党表示不服...
BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service with a 128-bit UUID (32 characters exclusive of dashes).   生成一个128位的UUID, 目的是生成一个service. 具体的方法, 参考ble的协议要求文档,core什么的...
// Long UUID denote custom user created UUID


// create switch characteristic and allow remote device to read and write

//下面就是创建一个对象的方法类似, 创建一个led的特征字, 可读可写
BLECharCharacteristic ledCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);  
// create button characteristic and allow remote device to get notifications

//下面创建一个按钮用的特征字, 可读可通知.
BLECharCharacteristic buttonCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); // allows remote device to get notifications
// Note use of Typed Characteristics. These previous 2  characeristics are of the type char

//setup是特么阿追诺的特色(shai)
void setup() {

  //设置串口9600
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output 设置对应的pin脚为输出
  pinMode(buttonPin, INPUT); // use button pin 4 as an input   button的pin脚是输入.

  // set the local name peripheral advertises   设置本地广播名, 问题是似乎设置了, 没用...
  blePeripheral.setLocalName("SmartJar");
  // set the UUID for the service this peripheral advertises:

  //设置外设的广播用UUID
  blePeripheral.setAdvertisedServiceUuid(ledService.uuid());

  // add service and characteristics

  //添加服务与特征字.
  blePeripheral.addAttribute(ledService);
  blePeripheral.addAttribute(ledCharacteristic);
  blePeripheral.addAttribute(buttonCharacteristic);

  // set initial values for led and button characteristic

  //设置初始值
  ledCharacteristic.setValue(0);
  buttonCharacteristic.setValue(0);

  // advertise the service

  //广播
  blePeripheral.begin();

  //串口打印.
  Serial.println("Bluetooth device active, waiting for connections...");
}

//这里就是一个大while (1)
void loop() {
  // poll peripheral

  //外设轮询
  blePeripheral.poll();

  // read the current button pin state

  //读取目前的按键pin状态
  char buttonValue = digitalRead(buttonPin);

  // has the value changed since the last read

  //看来要适应这种对象的写法. 判断button的值有没有改变.
  boolean buttonChanged = (buttonCharacteristic.value() != buttonValue);

  if (buttonChanged) {

  //如果按键改变值, 就更新特征字, 两个特征字同时改.
    // button state changed, update characteristics
    ledCharacteristic.setValue(buttonValue);
    buttonCharacteristic.setValue(buttonValue);
  }

  if (ledCharacteristic.written() || buttonChanged) {

  //写入任何, 或者按键改变, 都会在串口打印当前的LED灯的状态, 并实际性的改变LED的状态.
    // update LED, either central has written to characteristic or button state has changed
    // if you are using a phone or a BLE  central device that is aware of this characteristic, writing a value of 0x40 for example
    // Will be interpreted as written
    if (ledCharacteristic.value()) {
      Serial.println("LED on");
      digitalWrite(ledPin, HIGH);
    } else {
      // If central writes a 0 value then it is interpreted as no value and turns off the LED
      Serial.println("LED off");
      digitalWrite(ledPin, LOW);
    }
  }
}

原文地址:https://www.cnblogs.com/Montauk/p/5950061.html