I have a Genuino 101 which has a built in Bluetooth LE module. I have been trying to connect this to my phone using phonegap. I have been using cordova-plugin-bluetootle and with it I have been able to receive data from the arduino and display that data on the phone.
I tired adapting it so that I could send data from the phone to the arduino. I noticed in the CurieBle examples there was a project called "Callback LED" but when I connected to the Arduino from the Phone gap app I noticed in the debug message about characteristics it said "properties":{"notify": true, "read": true}
but nothing about write. I tried writing "Hello world" (converted into bytes then encoded string) I got an error similar to (I couldn't copy it exactly as it was an alert on the phone): "error": "write", "message": "Error Domain=CBATTErrorDomain Code = 3 \"Writing is not permitted.\" ..."...
There is more detail to the error message that is not at all relevant and is slightly repetitive.
Here is the arduino code (which is almost straight from the example):
#include <CurieBle.h>
const int ledPin = 13; // set ledPin to use on-board LED
BLEPeripheral blePeripheral; // create peripheral instance
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service
// create switch characteristic and allow remote device to read and write
BLECharCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
digitalWrite(ledPin, HIGH);
while(!Serial);
digitalWrite(ledPin, LOW);
// set the local name peripheral advertises
blePeripheral.setLocalName("LEDCB");
// set the UUID for the service this peripheral advertises
blePeripheral.setAdvertisedServiceUuid(ledService.uuid());
// add service and characteristic
blePeripheral.addAttribute(ledService);
blePeripheral.addAttribute(switchChar);
// assign event handlers for connected, disconnected to peripheral
blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
// assign event handlers for characteristic
switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten);
// set an initial value for the characteristic
switchChar.setValue(0);
// advertise the service
blePeripheral.begin();
Serial.println(("Bluetooth device active, waiting for connections..."));
}
void loop() {
// poll peripheral
blePeripheral.poll();
}
void blePeripheralConnectHandler(BLECentral& central) {
// central connected event handler
Serial.print("Connected event, central: ");
Serial.println(central.address());
}
void blePeripheralDisconnectHandler(BLECentral& central) {
// central disconnected event handler
Serial.print("Disconnected event, central: ");
Serial.println(central.address());
}
void switchCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
// central wrote new value to characteristic, update LED
Serial.print("Characteristic event, written: ");
if (switchChar.value()) {
Serial.println("LED on");
digitalWrite(ledPin, HIGH);
} else {
Serial.println("LED off");
digitalWrite(ledPin, LOW);
}
}
Here is the write function (phonegap) which is where the error is being produced from :
var string = "Hello World";
var bytes = bluetoothle.stringToBytes(string);
var encodedString = _arrayBufferToBase64(bytes);
alert("Service : " + connection.service + " char : " + connection.char + " Address: " + address);
var params = {"value":encodedString,"service":connection.service,"characteristic":connection.char,"address":address};
alert("params : " + JSON.stringify(params));
bluetoothle.write(function(res) {
alert("Success writing : " + JSON.stringify(res));
}, function(err) {
alert("Error writing: " + JSON.stringify(err));
}, params);
I think the error lies in the fact that the Arduino appears not to be advertising the "write" characteristic.
BTW the phone tells me that the service was 180d
and the characteristic was 2a37
.