Last active
June 22, 2023 11:17
-
-
Save Naveen-spritle/3c5881b7c5b75674eee4d338b80d8d49 to your computer and use it in GitHub Desktop.
Connecting the BLE weighing device by using device ID and get the displaying weight value of weighing device
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const [connectedDevice, setConnectedDevice] = useState(null); | |
const [connectedDeviceServices, setConnectedDeviceServices] = useState(null); | |
useEffect(() => { | |
let moniterCharacteristic; | |
let unSubscribeListener; | |
if (connectedDevice && connectedDeviceServices) { | |
// Device disconnect listener | |
unSubscribeListener = connectedDevice.onDisconnected((error, device) => { | |
alert("Device disconnected"); | |
setConnectedDevice(null); | |
setConnectedDeviceServices(null); | |
}); | |
connectedDeviceServices.forEach((service_data, index) => { | |
connectedDevice | |
.characteristicsForService(service_data.uuid) | |
.then((characteristics) => { | |
characteristics.forEach((characteristic_data) => { | |
if (characteristic_data.isNotifiable) { | |
moniterCharacteristic = | |
connectedDevice.monitorCharacteristicForService( | |
characteristic_data.serviceUUID, | |
characteristic_data.uuid, | |
(error, characteristic) => { | |
// error handling | |
if (error || !characteristic) { | |
alert( | |
"Something went wrong in moniter the characteristic" | |
); | |
return; | |
} else if (characteristic) { | |
var value = base64.decode(characteristic.value); // base64 is import from "react-native-base64" library for convert the base64 format to human readable string format | |
console.log(value) // Displaying value of weighing device | |
} | |
} | |
); | |
} | |
}); | |
}) | |
.catch((error) => { | |
alert( | |
"Something went wrong in get the characteristics for service" | |
); | |
}); | |
}); | |
} | |
return () => { | |
moniterCharacteristic && moniterCharacteristic.remove(); | |
unSubscribeListener && unSubscribeListener.remove(); | |
}; | |
}, [connectedDevice, connectedDeviceServices]); | |
//Connect BLE weighing device | |
const connectWeightMachine = (deviceId) => { | |
_BleManager | |
.connectToDevice(deviceId, { autoConnect: false }) | |
.then((device) => { | |
console.log("connected"); | |
console.log("device data - ",device) | |
device.discoverAllServicesAndCharacteristics().then(() => { | |
device | |
.services() | |
.then((device_services) => { | |
console.log("device services", device_services) | |
setConnectedDevice(device); | |
setConnectedDeviceServices(device_services); | |
}) | |
.catch((error) => { | |
console.log("service error ", error); | |
alert("Getting BLE service error"); | |
}); | |
}); | |
}) | |
.catch((error) => { | |
console.log("connect device error ", error); | |
alert("Connect to device error"); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment