I’m using capacitor-community ble plugin to integrate with a raspberry trough bluetooh.
Basically that model 3B is running a a script that expose a notification charactetistic that keep reading bytes from serial port and exporting it to bluetooth (every 8 bytes send one bluetooth value using notifications)
My Ionic app start and stop notifications many times and almost 95% works properly
But, sometimes we call startNotifications but we never receive the data back (sometimes we get a low voltage warn in rasp but we are using a 33w energy charger..)
Any clues on whats going on?
import { BleClient, BleDevice } from '@capacitor-community/bluetooth-le';
await BleClient.initialize();
await BleClient.getConnectedDevices(BALANCE_SERVICES)
.then(async (devices: BleDevice[]) => {
if (devices.length > 0) {
this.connectedDevice = devices[0];
}
this.ngZone.run(() => this.isBluetoothConnected = devices.length > 0);
});
await BleClient.startNotifications(
this.connectedDevice.deviceId,
this.moduleService.serviceUuid(),
this.moduleService.characteristicUuid(),
(data: DataView) => {
let weight = this.moduleService.parseWeight(data);
this.ngZone.run(() => {
this.lastReadingReceived = weight;
});
}
);
Python script ( just get_data section of )
def get_data(self):
value = []
firstRead = []
secondRead = []
isFirstRead = True
print('Initing get_data')
try:
ser = serial.Serial('/dev/ttyS0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
except Exception as e:
print('Failed to connect to serial %s', str(e))
return value
print('Connected to Serial')
while True:
try:
received = ser.read()
#print(received)
if isFirstRead is True:
firstRead.append(dbus.Byte(received))
else:
secondRead.append(dbus.Byte(received))
if b'r' in received and isFirstRead is False:
if firstRead == secondRead:
value = firstRead
break
else:
value = []
isFirstRead = True
firstRead = []
secondRead = []
pass
else:
if b'r' in received and isFirstRead is True:
isFirstRead = False
pass
except:
pass
print('Buffer received successfully')
ser.close()
print('Serial closed')
#print("%s" % ''.join([str(v) for v in value]))
length = len(value)
#Change that after development of LAN port reading
missingBytes = 16 - length
for x in range(missingBytes):
value.append(dbus.Byte(32))
return value