React Native Ble Manager: BLEMANATERDIDUPDATEVALUEFORCHARACTERISTISCHER HÖRSER NICHT SELDERUNGAndroid

Forum für diejenigen, die für Android programmieren
Anonymous
 React Native Ble Manager: BLEMANATERDIDUPDATEVALUEFORCHARACTERISTISCHER HÖRSER NICHT SELDERUNG

Post by Anonymous »

Ich verwende die React Native Ble Manager -Bibliothek, um Batterieinformationen von einem Gerät über Benachrichtigungen zu lesen. Der BLEMANAGERDIDUPDATEVALUEFORCHARACTERISTISCHE Hörer wird jedoch nicht ausgelöst, und ich sehe keine Protokolle im Hörer. PrettyPrint-Override "> export const readBatteryViaNotification = async (deviceId, SERVICEUUID, CHARACTERISTICUUID, onBatteryUpdate) => {
const batteryReadCommand = [0x22, 0x01]; // Command to request battery status

try {
// Start notification on the battery characteristic
try {
await BleManager.startNotification(deviceId, SERVICEUUID, CHARACTERISTICUUID);
console.log('🔔 Notifications started for battery');
} catch (error) {
console.error('❌ Failed to start notification:', error);
}

// Listen for updates on the battery characteristic
const subscription = bleManagerEmitter.addListener(
'BleManagerDidUpdateValueForCharacteristic',
({ value, peripheral, characteristic }) => {
console.log('🔔 Battery Notification Event:', { value, peripheral, characteristic });
if (peripheral !== deviceId || characteristic !== CHARACTERISTICUUID) return;

const buffer = Buffer.from(value);
console.log('🔋 Battery Notification Received:', buffer);

if (buffer.length < 2) {
console.warn('⚠️ Battery response too short');
return;
}

// Assuming battery voltage is in the first byte
const batteryVoltage = buffer[0] / 10.0;
// Assuming car battery voltage is in the second byte
const carBatteryVoltage = buffer[1] / 10.0;

// Pass battery info to the callback
onBatteryUpdate({
battery: batteryVoltage.toFixed(1),
carBattery: carBatteryVoltage.toFixed(1),
});
}
);

// Send command to the device to trigger battery reading
await BleManager.write(deviceId, SERVICEUUID, CHARACTERISTICUUID, batteryReadCommand);
console.log('📨 Battery read command sent');

return () => {
// Clean up the listener when done
subscription.remove();
console.log('🧹 Battery listener removed');
};
} catch (error) {
console.error('❌ Failed to read battery via notification:', error);
return () => {};
}
};< /code>
< /div>
< /div>
< /p>
Ich habe Protokolle im Hörer hinzugefügt, aber sie werden nicht gedruckt. Ich habe auch einen Screenshot der Dienste und Eigenschaften des Geräts angehängt. 0.77.0
Ble Manager Version: 11.5.7 < /p>
Ich bin seit über einer Woche in dieser Ausgabe festgefahren, und jede Hilfe wäre sehr geschätzt. < /P>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post