my.getBLEDeviceServices
Use this API to obtain all services of Bluetooth devices that are connected to the native.
Note:
Currently simulation in IDE is not supported. Please debug in production environment.
Sample Code
copy
//Obtain the services of the connected device
getBLEDeviceServices() {
my.getConnectedBluetoothDevices({
success: res => {
if (res.devices.length === 0) {
my.alert({ content: 'No connected devices' });
return;
}
my.getBLEDeviceServices({
deviceId: this.data.devid,
success: res => {
my.alert({ content: JSON.stringify(res) });
this.setData({
serid: res.services[0].serviceId,
});
},
fail: error => {
my.alert({ content: JSON.stringify(error) });
},
});
},
});
},
Return Value Sample
copy
{
"services": [{
"isPrimary": true,
"serviceId": "00001800-0000-1000-8000-00805f9b34fb"
}, {
"isPrimary": true,
"serviceId": "00001801-0000-1000-8000-00805f9b34fb"
}, {
"isPrimary": true,
"serviceId": "d0611e78-bbb4-4591-a5f8-487910ae4366"
}, {
"isPrimary": true,
"serviceId": "9fa480e0-4967-4542-9390-d343dc5d04ae"
}]
}
Parameters
Property | Type | Required | Description |
deviceId | String | Yes | The Bluetooth device ID. |
success | Function | No | The callback function for a successful API call. |
fail | Function | No | The callback function for a failed API call. |
complete | Function | No | The callback function used when the API call is completed. This function is always executed no matter the call succeeds or fails. |
Success return value
Property | Type | Description |
services | Array | List of discovered device services |
services
Property | Type | Description |
isPrimary | Boolean | This field indicates whether the service is the main service. Valid values are:
|
serviceId | String | The UUID of the service corresponding to a Bluetooth characteristic. |