APIs
All APIs:
- Request and Connect to Bluetooth Devices
 - Read a Bluetooth Characteristic
 - Write to a Bluetooth Characteristic
 - Start and stop GATT Notifications
 - Get disconnected from a Bluetooth Device
 
p5ble
// Interact with multiple devices, each new p5ble() only connect to one device
const myBLE = new p5ble();
const myOtherBLE = new p5ble();
Properties:
1. device
Once it’s connected successfully, .device will have all the information of the device
2. server
3. service
4. characteristics, array
Methods:
.connect(service, gotCharacteristics?)
Parameters:
- Service: Service UUID(e.g. 
0x1234,0x12345678,99999999-0000-1000-8000-00805f9b34fb) or Service Name(e.g.battery_service) - gotCharacteristics(optional): A callback function, e.g.
 
(err, characteristics) => { console.log(characteristics) }
Returns characteristics: array
.read(characteristic, dataType?, gotData?)
Parameters:
- characteristic: The characteristic UUID that you want to read
 - dataType (optional): default to 
uint8, options:custom,uint16,uint32,string,int8,int16,int32,float32,float64,float32-bigEndian,float64-bigEndian. - gotData(optional): A callback function, e.g.
 
(err, data) => { console.log(data) }
Returns data
Notes about dataType parameter
By default,
float32,float64will read data in little-endian format. If you passfloat32-bigEndianorfloat64-bigEndian, p5.ble.js will read data in big-endian format. Read more about Endianness.If you want to handle data parsing yourself in the
gotDatacallback, setdataTypetocustom
.write(characteristic, data)
Parameters:
- characteristic: The characteristic you want to write to
 - data: String
 
.startNotifications(characteristic, handleNotifications, dataType?)
Parameters:
- characteristic: The characteristic you want to start notification
 - handleNotifications: A callback function, e.g. (data) => { console.log(data) }
 - dataType (optional): default to 
uint8, options:custom,uint16,uint32,string,int8,int16,int32,float32,float64,float32-bigEndian,float64-bigEndian. 
Notes about dataType parameter
By default,
float32,float64will read data in little-endian format. If you passfloat32-bigEndianorfloat64-bigEndian, p5.ble.js will read data in big-endian format. Read more about Endianness.If you want to handle data parsing yourself in the
handleNotificationscallback, setdataTypetocustom
.stopNotifications(characteristic)
Parameters:
- characteristic: The characteristic you want to stop notification
 
.disconnect()
.onDisconnected(handleDisconnected)
Parameters:
- handleDisconnected: A callback function, e.g. (device) => { console.log('Device ' + device.name + ' is disconnected.'); }
 
.isConnected()
returns a Boolean