import React, { useEffect, useState } from 'react';
import { DeviceEventEmitter } from 'react-native';
import BleManager from 'react-native-ble-manager';
const ConnectedDevice = () => {
const [currentDevice, setCurrentDevice] = useState(null);
const [receivedData, setReceivedData] = useState(null);
// Initialize BleManager
useEffect(() => {
const initializeBleManager = async () => {
await BleManager.start({ showAlert: false });
console.log('Module initialized');
};
initializeBleManager();
}, []);
// Connect to a BLE device
const connectToDevice = async (deviceId) => {
try {
// Connect to the device
await BleManager.connect(deviceId);
console.log(`Connected to device: ${deviceId}`);
// Retrieve services and characteristics
const peripheralInfo = await BleManager.retrieveServices(deviceId);
setCurrentDevice(peripheralInfo);
const serviceUUID = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E";
const characteristicUUID = "6E400003-B5A3-F393-E0A9-E50E24DCCA9E";
// Start notifications for the characteristic
await BleManager.startNotification(peripheralInfo.id, serviceUUID, characteristicUUID);
console.log('Notification started for characteristic:', characteristicUUID);
} catch (error) {
console.error('Connection error:', error);
}
};
// Listen for characteristic updates
useEffect(() => {
const characteristicValueUpdate = DeviceEventEmitter.addListener(
'BleManagerDidUpdateValueForCharacteristic',
(data) => {
// Handle received data
readCharacteristicFromEvent(data);
}
);
// Cleanup the event listener on component unmount
return () => {
characteristicValueUpdate.remove();
};
}, []);
// Read and process data from the characteristic
const readCharacteristicFromEvent = (data) => {
const { characteristic, value } = data;
console.log('Received data:', value);
setReceivedData(value);
};
return (
// Your component JSX here
<></>
);
};
export default ConnectedDevice;
I am developing a React Native application using the react-native-ble-manager library to communicate with a Bluetooth Low Energy (BLE) device. While I can connect to the device successfully, I am having trouble receiving notifications from a specific characteristic. I have verified that the characteristic UUID is correct, but I am not receiving any data in the notification listener.
I have set up notifications for a BLE characteristic with the following UUID: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E. Although the notification is successfully started (as confirmed by console logs), I do not receive any data when the device sends it.
govardhan ob is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.