question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to handle Indications (Question)

See original GitHub issue

Hi,

I’m trying to connect to my Glucose meter device and read my latest measurements. I can discover/connect to the device using this library. My device supports the Bluetooth standard about the Glucose devices (I can see the right service uuid’s when I connect to the device).

After I have successfully connect my device to my app,

BleManager.connect(mac_adress) .then((peripheralInfo) => { console.log(peripheralInfo); }) .catch((error) => { console.log(error); });

I’m enabling the notifications on the Glucose Measurement characteristic,

BleManager.startNotification(mac, "00001808-0000-1000-8000-00805F9B34FB", "00002a18-0000-1000-8000-00805F9B34FB") .then(() => { console.log('GLUCOSE MEASUREMENT NOTIFICATION STARTED'); }) .catch((error) => { console.log(error); });

and then I’m supposed to enable the indications on the RECORD ACCESS CONTROL POINT characteristic, and then follow up by writing some data to the same characteristic that will inform the device on how many records I want to read for example.

Currently I have no idea how to enable indications on this service/characteristic. I’ve tried using the startNotification method again, but I get this error:

Failed to set client characteristic notification for 00002a52-0000-1000-8000-00805f9b34fb

Do you know how I can enable indications on a BLE device? To be honest I’m not 100% sure that these steps are the ones that I have to follow in order to read the measurements, but I can’t even try this method.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ekaratzaferiscommented, May 20, 2017

Hi and sorry for the delay!

To be honest, after connecting to the device, I gave up on this project, so I haven’t implemented any functions for reading/writing data to the device.

Also, the connect method is truly ugly, you should do something about it!

By the way, I was learning React Native + Redux + ECMAScript 6 at the time, so the code is messed up (a lot) 😕

import BleManager from 'react-native-ble-manager'
import { NativeAppEventEmitter } from 'react-native'

const ble_api = {

    // Variables //
    scanning: {
        active: false,
        interval: null,
    },
    device: {
        found: false,
        info: {
            name: '',
            id: ''    
        }
    },

    // Bind listener for the discovery event //
    initiate: function (discovery_handler) {
        BleManager.start({showAlert: false})
        this.peripheral_found = this.peripheral_found.bind(this, discovery_handler)
        NativeAppEventEmitter.addListener('BleManagerDiscoverPeripheral', this.peripheral_found)
    },

    // Check if bluetooth is activated on the device and run the handler //
    is_on: function (handler) {
        BleManager.enableBluetooth().then(() => {
            handler('on')
        }).catch((error) => {
            handler('off')
        })
    },

    // Toggle device scanning //
    toggle_scan: function (bool) {
        if (bool) {
            this.scanning.active = true
            this.device.found = false
            this.scanning.interval = setInterval(() => 
                BleManager.scan([], 30, true).then((results) => this.device.found = true), 3000)
        } else{
            BleManager.stopScan().then(() => this.scanning.active = false)
            clearInterval(this.scanning.interval)
        }
    },

    // Execute assigned handler //
    peripheral_found: function (handler,data) {
        // Stop device scanning and connect to the peripheral //
        this.toggle_scan(false)
        this.connect(data.id)
        // Connect to the device //
        this.device.info = data
        // Run handler //
        handler(data)
    },

    // Connect //
    connect: function(mac){
        BleManager.connect(mac)
        .then((peripheralInfo) => {
            // Success code //
            console.log(peripheralInfo);
            BleManager.startNotification(mac, "00001808-0000-1000-8000-00805F9B34FB", "00002a18-0000-1000-8000-00805F9B34FB",)
            .then(() => {
                console.log('CLUCOSE MEASUREMENT NOTIFICATIONS: ON');
                NativeAppEventEmitter.addListener('BleManagerDidUpdateValueForCharacteristic', function(data){ 
                    console.log('RESULT FROM CLUCOSE MEASUREMENT NOTIFICATION')
                    console.log(data) 
                })
                setTimeout(function() {
                    BleManager.startNotification(mac, "00001808-0000-1000-8000-00805F9B34FB", "00002a34-0000-1000-8000-00805F9B34FB")
                    .then(() => {
                        console.log('CLUCOSE MEASUREMENT CONTEXT NOTIFICATIONS: ON');
                        NativeAppEventEmitter.addListener('BleManagerDidUpdateValueForCharacteristic', function(data){ 
                            console.log('RESULT FROM CLUCOSE MEASUREMENT CONTEXT NOTIFICATION')
                            console.log(data)
                        })
                        setTimeout(function() {
                            BleManager.startNotification(mac, "00001808-0000-1000-8000-00805F9B34FB", "00002a52-0000-1000-8000-00805F9B34FB")
                            .then(() => {
                                console.log('SUBSCRIBING TO RAPC')
                                NativeAppEventEmitter.addListener('BleManagerDidUpdateValueForCharacteristic', function(data){ 
                                    console.log('RESULT FROM RAPC NOTIFICATION')
                                    console.log(data)
                                });
                                setTimeout(function() {
                                    var myData = [0x01,0x01]
                                    var base64 = require('base64-js')
                                    var data = base64.fromByteArray(myData)
                                    BleManager.write(mac, "00001808-0000-1000-8000-00805F9B34FB", "00002a52-0000-1000-8000-00805F9B34FB",data)
                                    .then(() => {
                                        console.log('Write' + data)
                                    }).catch((error) => { console.log(error) })
                                }, 3000); 
                            }).catch((error) => { console.log(error) });
                        }, 3000); 
                    }).catch((error) => { console.log(error) })
                }, 3000);
            }).catch((error) => { console.log(error) })
        }).catch((error) => { console.log(error) })
    },

    // Read measurement from device //
    read: function(){
    
    },

    // Read all measurements from device //
    read_all: function(){
        
    },
    // Send data to the device //
    write: function(){
        
    }
};
module.exports = ble_api

If I recall correctly my train of thought was something like this: Use is_on to check if bluetooth is turned on the device and then use initiate I had a button assigned to toggle device scanning with toggle_scan peripheral_found was executed upon discovery connect to the device by adding some ugly intervals between the “commands”

I did get some data back from the device by I didn’t bother to continue at the time.

Hope this gives you an idea on how to write your API, and maybe share your work with others too 😃

0reactions
Utkarshlakheracommented, Jan 22, 2022

@stephenjen Your above issue fixed? if yes, so can you tell me that how we can resolve it. I am getting issue in the android device. Failed to set client characteristic notification for “charID(xyz)”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Guidelines for Answering Questions - Buck Benedict
Be honest, don't be afraid to say you don't know the answer; if appropriate, say you'll get the answer and get back to...
Read more >
How to handle a Q&A session - Management-Issues
Calm and courteous: Deal with all questioners thoughtfully and courteously. Never ignore someone whose question is not pertinent or who you feel ...
Read more >
Frequently Asked Questions: Geographical Indications - WIPO
Questions and answers on geographical indications. Covers basics, applying for geographical indication protection, and business issues.
Read more >
Guidelines for Handling Questions and Answer Sessions
List every conceivable question and answer ahead of time. Ask someone to drill you. Hold some research in reserve, on a visual or...
Read more >
Writing Strong Research Questions | Criteria & Examples
Research questions give your project a clear focus. They should be specific and feasible, but complex enough to merit a detailed answer.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found