jasongin / noble-uwp

Noble (Node.js Bluetooth LE) with Windows 10 UWP bindings
MIT License
83 stars 45 forks source link

Subsequent reads of a characteristic return a cached value #48

Closed acstacey closed 6 years ago

acstacey commented 6 years ago

The connection and characteristic discovery are working and I'm assigning the discovered characteristic to a variable to do the reading. The first read using the snippet below is returning the correct value but every subsequent read returns immediately with the same value as the first read. I've verified what the read should return with Bluetooth LE Explorer: https://github.com/Microsoft/BluetoothLEExplorer.

Thanks

export async function readCharacteristic(id) {
    if (!botIsConnected() || gattIsBusy) {
        return false;
    }
    let charac = bleCharacFromID(id);
    if (!charac) {
        console.log('Invalid BLE characteristic on write');
        return false;
    }
    gattIsBusy = true;
    let retVal = await readCharAsync(charac);
    gattIsBusy = false;
    console.log('retVal: ' + retVal);
    return retVal.readUInt8(0);
}

async function readCharAsync(charac) {
    return await new Promise(function(resolve,reject){
         charac.read(function(err, data) {
             if(err !== null) return reject(err);
             resolve(data);
         });
    });
}

Update: I found the bluetooth characteristic read call that didn't have the uncached flag set. The pull request fixes the issue for me. I'm not sure if caching is the desired behaviour and maybe there is another way to force an update.