Hi, i'm trying to use this library for my application.
I follow the guide on your site (https://www.polidea.com/blog/ReactNative_and_Bluetooth_to_An_Other_level/ )
I managed to make the connection with the device but when I try to get the notification depending on the uuid I get this kind of error
"ERROR: Service 0000F0F1-0000-1000-8000-00805F9B34FB for device ? not found"
Now i don't understand how to do with the the notification (the service number is right. )
Thank you.
This is the code:
export default class Main extends Component {
constructor() {
super();
this.manager = new BleManager()
this.state = {info: "", values: {}}
this.prefixUUID = "0000F0F"
this.suffixUUID = "-0000-1000-8000-00805F9B34FB"
this.sensors = {
1: "Accelerometer",
2: "Humidity",
3: "Magnetometer",
4: "Barometer",
5: "Gyroscope"
}
}
serviceUUID(num) {
return this.prefixUUID + num + this.suffixUUID
}
notifyUUID(num) {
return this.prefixUUID + num + this.suffixUUID
}
writeUUID(num) {
return this.prefixUUID + num + this.suffixUUID
}
scanAndConnect(){
this.manager.startDeviceScan(null, null, (error, device) => {
if (error) {
return
}
let model = '0030'; // this is an example
if (device.name == this.model_dx(model) ) {
this.manager.stopDeviceScan();
device.connect()
.then((device) => {
this.info("Discovering services and characteristics ")
return device.discoverAllServicesAndCharacteristics()
})
.then((device) => {
this.info("Setting notifications")
return this.setupNotifications(device)
})
.catch((error) => {
this.error(error.message)
});
}
});
}
async setupNotifications(device) {
for (const id in this.sensors) {
const service = this.serviceUUID(id)
const characteristicW = this.writeUUID(id)
const characteristicN = this.notifyUUID(id)
const characteristic = await device.readCharacteristicForService(service, characteristicN)
device.monitorCharacteristicForService(service, characteristicN, (error, characteristic) => {
if (error) {
this.error(error.message)
return
}
this.updateValue(characteristic.uuid, characteristic.value)
})
}
}
........
.......
render() {
return (
<View>
<Text>{this.state.info}</Text>
{Object.keys(this.sensors).map((key) => {
return <Text key={key}>
{this.sensors[key] + ": " + (this.state.values[this.notifyUUID(key)] || "-")}
</Text>
})}
</View>
)
}
Hi, I am facing similar issue. Does it have anything to do with Bluetooth pairing password/ pin?
I don't think, I have no password / pin. I managed to make the connection but I can't get the services because it gives me that error
I am having a similar problem. After running discoverAllServicesAndCharacteristicsForDevice I can write to a characteristic, but when I try to read services or characteristics for the connected device I am getting the error reported here.
Have you checked what services/characteristics are available?
Yes and I was getting an empty Array.
For me, I wonder if I might be using an old instance of the device. Looks like certain calls take a device ID but instantiate a new device model. I was holding the device in props and passing the id to BleManager.
I fixed this for connectToDevice but looks like I need it for discoverAllServicesAndCharacteristicsForDevice too. Do I need to get the new device back from all of these calls? Are there certain ones to look out for?
That was my problem. I was not using the updated device returned by bleManager.discoverAllServicesAndCharacteristicsForDevice(). Storing the returned device and using that device.id for a later characteristic query fixed this error for me.
The only thing that can be persisted between creations of the client is the deviceId. After each connection to a BLE peripheral one _must_ perform a service discovery before using characteristics
Oh okay, sounds like saving the device id and access everything via bleManager is the best way to go then. Thanks!
@dariuszseweryn I follow the guide in the polidea site but i receive always the error.
You can see the code I used in the first discussion. I don't understand why it continues to give me the error. In theory the services / characteristics are right. Only it still can't find them. What can it be?
I still think that the library is correct. There is little chance that the service and characteristic have the exact same UUID.
I'm sorry I didn't understand. Do you think there are problems in the code?
Could it be that the services and features cannot be written but only read?
Compare your serviceUUID(), notifyUUID(), writeUUID(). They are all the same. There is _little_ chance that serviceUUID and notifyUUID/writeUUID should be the same. Try listing all of the services that were discovered and all characteristics that are contained in their respective services. You will see for yourself
Ok I'm new in this world, so I'm sorry if I don't understand well and I'll ask you "stupid" questions.
In essence, I have a uuid for the characteristics of Movement (Accelerometer, Gyroscope).
My problem is that my uuid as you can see in the code does not allow me to add
Try listing the services _after_ the service discovery is performed. It could go something along these lines:
async function printAllServices(device: Device) {
const services: [Service] = await device.services();
for (let i = 0; i < services.length; i++) {
const service: Service = services[i];
const characteristics: [Characteristic] = await service.characteristics();
for (let j = 0; j < characteristics.length; j++) {
const char: Characteristic = characteristics[j];
console.log(`Characteristic UUID: ${char.uuid}, Service UUID: ${char.serviceUUID}`);
}
}
}
And show the output
I have insert your code there:
scanAndConnect(){
this.manager.startDeviceScan(null, null, (error, device) => {
if (error) {
/* nel caso di un errore nella scansione */
return
}
/* la variabile model si deve recuperare */
let model = '0030';
if (device.name == this.model_dx(model) ) {
this.manager.stopDeviceScan();
device.connect()
.then((device) => {
this.info("Discovering services and characteristics ")
return device.discoverAllServicesAndCharacteristics()
})
.then((device) => {
this.info("Discovering")
return this.printAllServices(device)
})
.catch((error) => {
this.error(error.message)
});
}
});
}
/* Chiusura Scansione e Connessione */
async printAllServices(device: Device) {
const services: [Service] = await device.services();
for (let i = 0; i < services.length; i++) {
const service: Service = services[i];
const characteristics: [Characteristic] = await service.characteristics();
for (let j = 0; j < characteristics.length; j++) {
const char: Characteristic = characteristics[j];
console.log(`Characteristic UUID: ${char.uuid}, Service UUID: ${char.serviceUUID}`);
}
}
}
But i received error about: Device, Service, Characteristic
These are flow annotations. Feel free to remove them : type
Hi, these are all the UUID that i found with the console.log:
05-23 10:15:56.817 21635 21682 I ReactNativeJS: Characteristic UUID: 00002a00-0000-1000-8000-00805f9b34fb, Service UUID: 00001800-0000-1000-8000-00805f9b34fb
05-23 10:15:56.817 21635 21682 I ReactNativeJS: Characteristic UUID: 00002a01-0000-1000-8000-00805f9b34fb, Service UUID: 00001800-0000-1000-8000-00805f9b34fb
05-23 10:15:56.817 21635 21682 I ReactNativeJS: Characteristic UUID: 00002a04-0000-1000-8000-00805f9b34fb, Service UUID: 00001800-0000-1000-8000-00805f9b34fb
05-23 10:15:56.830 21635 21682 I ReactNativeJS: Characteristic UUID: 00002a05-0000-1000-8000-00805f9b34fb, Service UUID: 00001801-0000-1000-8000-00805f9b34fb
05-23 10:15:56.857 21635 21682 I ReactNativeJS: Characteristic UUID: 0000f0f1-0000-1000-8000-00805f9b34fb, Service UUID: 0000f0f0-0000-1000-8000-00805f9b34fb
05-23 10:15:56.857 21635 21682 I ReactNativeJS: Characteristic UUID: 0000f0f2-0000-1000-8000-00805f9b34fb, Service UUID: 0000f0f0-0000-1000-8000-00805f9b34fb
05-23 10:15:56.857 21635 21682 I ReactNativeJS: Characteristic UUID: 0000f0f3-0000-1000-8000-00805f9b34fb, Service UUID: 0000f0f0-0000-1000-8000-00805f9b34fb
05-23 10:15:56.857 21635 21682 I ReactNativeJS: Characteristic UUID: 0000f0f4-0000-1000-8000-00805f9b34fb, Service UUID: 0000f0f0-0000-1000-8000-00805f9b34fb
05-23 10:15:56.857 21635 21682 I ReactNativeJS: Characteristic UUID: 0000f0f5-0001-0008-0000-0805f9b34fb0, Service UUID: 0000f0f0-0000-1000-8000-00805f9b34fb
05-23 10:15:56.857 21635 21682 I ReactNativeJS: Characteristic UUID: 0000f0f6-0001-0008-0000-0805f9b34fb0, Service UUID: 0000f0f0-0000-1000-8000-00805f9b34fb
05-23 10:15:56.857 21635 21682 I ReactNativeJS: Characteristic UUID: 0000f0f7-0001-0008-0000-0805f9b34fb0, Service UUID: 0000f0f0-0000-1000-8000-00805f9b34fb
05-23 10:15:56.857 21635 21682 I ReactNativeJS: Characteristic UUID: 0000f0f8-0001-0008-0000-0805f9b34fb0, Service UUID: 0000f0f0-0000-1000-8000-00805f9b34fb
05-23 10:15:56.857 21635 21682 I ReactNativeJS: Characteristic UUID: 0000f0f9-0001-0008-0000-0805f9b34fb0, Service UUID: 0000f0f0-0000-1000-8000-00805f9b34fb
05-23 10:15:56.857 21635 21682 I ReactNativeJS: Characteristic UUID: 0000f0fa-0001-0008-0000-0805f9b34fb0, Service UUID: 0000f0f0-0000-1000-8000-00805f9b34fb
05-23 10:15:56.857 21635 21682 I ReactNativeJS: Characteristic UUID: 0000f0fb-0000-1000-8000-00805f9b34fb, Service UUID: 0000f0f0-0000-1000-8000-00805f9b34fb
05-23 10:15:56.857 21635 21682 I ReactNativeJS: Characteristic UUID: 0000f0fc-0000-1000-8000-00805f9b34fb, Service UUID: 0000f0f0-0000-1000-8000-00805f9b34fb
05-23 10:15:56.858 21635 21682 I ReactNativeJS: Characteristic UUID: 0000f0fd-0001-0008-0000-0805f9b34fb0, Service UUID: 0000f0f0-0000-1000-8000-00805f9b34fb
05-23 10:15:56.862 21635 21682 I ReactNativeJS: Characteristic UUID: 00002a19-0000-1000-8000-00805f9b34fb, Service UUID: 0000180f-0000-1000-8000-00805f9b34fb
05-23 10:15:56.873 21635 21682 I ReactNativeJS: Characteristic UUID: 00002a29-0000-1000-8000-00805f9b34fb, Service UUID: 0000180a-0000-1000-8000-00805f9b34fb
05-23 10:15:56.873 21635 21682 I ReactNativeJS: Characteristic UUID: 00002a27-0000-1000-8000-00805f9b34fb, Service UUID: 0000180a-0000-1000-8000-00805f9b34fb
05-23 10:15:56.873 21635 21682 I ReactNativeJS: Characteristic UUID: 00002a26-0000-1000-8000-00805f9b34fb, Service UUID: 0000180a-0000-1000-8000-00805f9b34fb
05-23 10:15:56.873 21635 21682 I ReactNativeJS: Characteristic UUID: 00002a28-0000-1000-8000-00805f9b34fb, Service UUID: 0000180a-0000-1000-8000-00805f9b34fb
05-23 10:15:56.873 21635 21682 I ReactNativeJS: Characteristic UUID: 00002a23-0000-1000-8000-00805f9b34fb, Service UUID: 0000180a-0000-1000-8000-00805f9b34fb
05-23 10:15:56.873 21635 21682 I ReactNativeJS: Characteristic UUID: 00002a24-0000-1000-8000-00805f9b34fb, Service UUID: 0000180a-0000-1000-8000-00805f9b34fb
So does anything look suspicious to you?
Maybe that many of the services are the same?
I should use:
Characteristic UUID: 0000f0fd-0001-0008-0000-0805f9b34fb0,
Service UUID: 0000f0f0-0000-1000-8000-00805f9b34fb
Characteristic UUID: 00002a19-0000-1000-8000-00805f9b34fb,
Service UUID: 0000180f-0000-1000-8000-00805f9b34fb
But the UUIDs are Completely different
Different to what?
If I wanted to use the example code shown on your site, it would be quite complicated (I think) to use it.
For example regarding:
Characteristic UUID: 00002a19-0000-1000-8000-00805f9b34fb,
Service UUID: 0000180f-attractions1000-8000-00805f9b34fb
In
this.sensors = {
// i should use 180 for the service
180: "name"
}
but the number services( 0000180f) and the characterist(00002a19) number are complety different.
Did I understand how to use it?
So they are indeed different. Remember that samples are just that — samples — to show the idea. But we are getting too far from the original problem:
"ERROR: Service 0000F0F1-0000-1000-8000-00805F9B34FB for device ? not found"
Now i don't understand how to do with the the notification (the service number is right. )
and
05-23 10:15:56.857 21635 21682 I ReactNativeJS: Characteristic UUID: 0000f0f1-0000-1000-8000-00805f9b34fb, Service UUID: 0000f0f0-0000-1000-8000-00805f9b34fb
and
serviceUUID(num) {
return this.prefixUUID + num + this.suffixUUID
}
notifyUUID(num) {
return this.prefixUUID + num + this.suffixUUID
}
Ok let's considered this one:
Characteristic UUID: 0000f0f4-0000-1000-8000-00805f9b34fb
Service UUID: 0000f0f0-0000-1000-8000-00805f9b34fb
this.prefixUUID = "0000"
this.suffixUUID = "-0000-1000-8000-00805f9b34fb"
this.sensors = {
f0f: "Example"
}
}
serviceUUID(num) {
return this.prefixUUID + num + "0" + this.suffixUUID
}
notifyUUID(num) {
return this.prefixUUID + num + "4" + this.suffixUUID
}
This is the only method?? The problem is that i can't add in this.sensors other sensors.
The most straightforward solution is:
this.prefixUUID = "0000FOF"
this.suffixUUID = "-0000-1000-8000-00805f9b34fb"
this.sensors = {
//...
4: "Barometer"
//...
}
serviceUUID(num) {
return this.prefixUUID + "0" + this.suffixUUID // it is always the same for all listed this.sensors
}
notifyUUID(num) {
return this.prefixUUID + num + this.suffixUUID
}
Ok I don't want to bore you more xD
I had thought of using a situation you proposed but there is a problem:
Basically this is all my code:
export default class Main extends Component {
constructor() {
super();
this.manager = new BleManager()
this.state = {info: "", values: {}}
this.prefixUUID = "0000F0F"
this.suffixUUID = "-0000-1000-8000-00805f9b34fb"
this.suffixUUIDs = "-0001-0008-0000-0805f9b34fb0"
this.sensors = {
5: "Acc+Gyro+Mag"
}
}
serviceUUID(num) {
return this.prefixUUID + "0" + this.suffixUUID
}
notifyUUID(num) {
return this.prefixUUID + num + this.suffixUUIDs
}
info(message) {
this.setState({info: message})
}
error(message) {
this.setState({info: "ERROR: " + message})
}
updateValue(key, value) {
this.setState({values: {...this.state.values, [key]: value}})
}
componentWillMount(){
const subscription = this.manager.onStateChange((state) => {
if (state === 'PoweredOn') {
this.scanAndConnect();
subscription.remove();
}
}, true);
}
async requestPermission() {
try {
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
this.setState({permissionStatus:'granted'});
}else if(granted === PermissionsAndroid.RESULTS.DENIED) {
this.setState({permissionStatus:'denied'});
}else{
}
} catch (err) {
console.error(err)
}
}
scanAndConnect(){
this.manager.startDeviceScan(null, null, (error, device) => {
if (error) {
return
}
....... // Connection to device // ......
this.manager.stopDeviceScan();
device.connect()
.then((device) => {
this.info("Discovering services and characteristics ")
return device.discoverAllServicesAndCharacteristics()
})
.then((device) => {
this.info("SetupNotification")
return this.setupNotifications(device)
})
.catch((error) => {
this.error(error.message)
});
}
});
}
async setupNotifications(device) {
for (const id in this.sensors) {
const service = this.serviceUUID(id)
const characteristicN = this.notifyUUID(id)
device.monitorCharacteristicForService(service, characteristicN, (error, characteristic) => {
if (error) {
this.error(error.message)
return
}
this.updateValue(characteristic.uuid, characteristic.value)
})
}
}
render() {
return (
<View>
<Text>{this.state.info}</Text>
{Object.keys(this.sensors).map((key) => {
return <Text key={key}>
{this.sensors[key] + ": " + (this.state.values[this.notifyUUID(key)] || "-")}
</Text>
})}
</View>
)
}
}
Using your advice, when I run the app it blocks at
this.info("SetupNotification")
Maybe the problem is that there isn't "num" in:
serviceUUID(num) {
return this.prefixUUID + "0" + this.suffixUUID
}
¯\_(ツ)_/¯ It is a trivial programming issue. There are plenty of possible solutions which I expect you can handle at this point
this.sensors = {
// ...
"0000f0f4-0000-1000-8000-00805f9b34fb": "Barometer",
"0000f0f5-0001-0008-0000-0805f9b34fb0": "Gyroscope", // looks like an awful typo in the UUID
// ...
}
serviceUUID() {
return "0000f0f0-0000-1000-8000-00805f9b34fb"
}
Excuse me but this is not the best place to look for help in debugging your app. You could try using https://codereview.stackexchange.com/ or https://stackoverflow.com I suppose.
The issue you opened is not an issue with the library. The library clearly stated that a UUID you passed is not available. I have shown how you can easily list the available characteristics UUIDs and UUIDs of their respective services. From this point in it all about how you will pass them to the library to use
Ok thank you for your help. =)
"0000f0f5-0001-0008-0000-0805f9b34fb0": "Gyroscope", // looks like an awful typo in the UUID
I know it also seems to me an error (in fact in the pdf of the device it is not so) but going to do the scanning of the services and the characteristics results in this way.
Anyway, I ask you the last question and then we can close this topic :)
It's possible to connect to two device simultanely concurrently??
In my case i have two devices(one right and one left)
https://github.com/Polidea/react-native-ble-plx/wiki/Device-Connecting
It is possible to have a few connections established at the same time.
Ok so it's possible. Thanks!
I guess I can close now