noble / bleno

A Node.js module for implementing BLE (Bluetooth Low Energy) peripherals
MIT License
2.12k stars 447 forks source link

2 devices cannot be subscribed on one GATT Characteristic #419

Open nagasiNR opened 5 years ago

nagasiNR commented 5 years ago

I try to implement RaspberryPI (BLE device) with a notification Characteristic

When I connect the 1st device (Android) to BLE device and subscribe it on notifications characteristic all work ok. But then I do the same for the 2nd device (iOS) Maybe OS doesn't matter.

Expected result: 2 devices stay subscribed and get notifications Actual result: 1st device loses notifications

I feel problem in onSubscribe function and updateValueCallback callback In console I haven't any errors

PS. I have tried push updateValueCallback in an array in onSubscribe function and do update forEach callback, but it works with the same problem + in this case I get problem with unsubscribe device, because In BLE device I haven't information which device is connected.

My implementation: `const NOTIFICATIONS_SERVICE_UUID = '5292' const NOTIFICATIONS_CHAR_UUID = 'e6c5'

class NotificationsCharacteristic extends bleno.Characteristic { constructor() { super({ uuid: NOTIFICATIONS_CHAR_UUID, properties: ['read', 'notify'], value: null });

    this.notificationId = 0;
}

onSubscribe(maxValueSize, updateValueCallback) {
    console.log('NotificationCharacteristic - onSubscribe');

    this.updateValueCallback = updateValueCallback;
}

onNotify() {
    console.log('NotificationCharacteristic - onNotify');
}

onUnsubscribe() {
    console.log('NotificationCharacteristic - onUnsubscribe');
}

sendNotification(value) {
    if (this.updateValueCallback) {
        console.log("Sending notification with value: ', value);
        const notificationBytes = new Buffer(2);
        notificationBytes.writeUInt16BE(value);

        this.updateValueCallback(notificationBytes);
    }
}

startNotifications() {
    console.log('Starting notifications');

    this.handle = setInterval(() => {
        if (this.notificationId >= 8) {
            this.notificationId = 1;
        } else {
            this.notificationId++;
        }

        this.sendNotification(this.notificationId);
    }, 7000);
}

stopNotifications() {
    console.log('Stopping notifications');
    clearInterval(this.handle);
    this.handle = null;
}

}

let notifications = new NotificationsCharacteristic(); notifications.startNotifications();

bleno.on('advertisingStart', function (error) { if (error) { console.log('advertisingStart Error: ' + error); } else { console.log('advertisingStart: STARTED');

    let service = new bleno.PrimaryService({
        uuid: NOTIFICATIONS_SERVICE_UUID,
        characteristics: [notifications]
    })

    bleno.setServices([service]);
}

}); `

I work with Ionic and connect to BLE device by ionic-ble plugin by next code

this.ble.connect(deviceId).subscribe((connectData) => { console.log('connectData: ', connectData); this.ble.startNotification(deviceId, NotificationServiceId, NotificationCharacteristicId) .subscribe((arrayBuffer) => { // show notification });

Thanks!

summerswell-ie commented 4 years ago

Did you ever get this working? I am getting a similar issue when connecting from Android and iOS, whereas two Android devices work without a problem.