Arduino 101/Genuino 101 蓝牙4.0 BLE接收int类型数据

Arduino 101/Genuino 101板子与arduino 相比多了一些功能,其中一个就是多了一块4.0的蓝牙BLE。

4.0蓝牙接收数据的协议与2.0的有所不同,更加复杂。

如下代码为101板子自带的BLE模块接收int类型数据源码。

#include 

BLEPeripheral blePeripheral; 

BLEService lightService("19B10000-E8F2-537E-4F6C-D104768A1214"); 
BLEIntCharacteristic   switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

void setup() {
  
  Serial.begin(9600);
  blePeripheral.setLocalName("LIU_BLE_01");
  blePeripheral.setAdvertisedServiceUuid(lightService.uuid());

  blePeripheral.addAttribute(lightService);
  blePeripheral.addAttribute(switchCharacteristic);

  switchCharacteristic.setValue(0);
  blePeripheral.begin();
  Serial.println("BLE Light service.");
}

void loop() {

  BLECentral central = blePeripheral.central();

  if (central) {
    Serial.print("Connected to central: ");
    Serial.println(central.address());

    while (central.connected()) {
      if (switchCharacteristic.written()) {
          Serial.println(switchCharacteristic.value());
      }
    }
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
} 


你可能感兴趣的:(Arduino)