Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't read characteristic value from peripheral #13

Open
nickcom opened this issue Apr 3, 2019 · 1 comment
Open

can't read characteristic value from peripheral #13

nickcom opened this issue Apr 3, 2019 · 1 comment

Comments

@nickcom
Copy link

nickcom commented Apr 3, 2019

I'm unable to get the example ble central code working with the example ble peripheral code. I'm able to successfully scan and connect to the peripheral from the central device (both iPhones) but I am unable to read the characteristic value from the peripheral. I get no reply.

Any suggestions would be greatly appreciated!

central code:

'use strict';

var app = {
initialize: function() {
    this.bindEvents();
    detailPage.hidden = true;
},
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
    refreshButton.addEventListener('touchstart', this.refreshDeviceList, false);
    batteryStateButton.addEventListener('touchstart', this.readCharacteristic, false); //**new
    disconnectButton.addEventListener('touchstart', this.disconnect, false);
    deviceList.addEventListener('touchstart', this.connect, false); // assume not scrolling
},
onDeviceReady: function() {
    app.refreshDeviceList();
},
refreshDeviceList: function() {
    deviceList.innerHTML = ''; // empties the list
    // scan for all devices
    ble.scan(['FF10'], 5, app.onDiscoverDevice, app.onError); //**new
},
onDiscoverDevice: function(device) {

    console.log(JSON.stringify(device));
    var listItem = document.createElement('li'),
    html = '<b>' + device.name + '</b><br/>' +
    'RSSI: ' + device.rssi + '&nbsp;|&nbsp;' +
    device.id;

    listItem.dataset.deviceId = device.id;  // TODO
    listItem.innerHTML = html;
    deviceList.appendChild(listItem);

},
connect: function(e) {
    var deviceId = e.target.dataset.deviceId,

    onConnect = function() {
        batteryStateButton.dataset.deviceId = deviceId;
        disconnectButton.dataset.deviceId = deviceId;
        app.showDetailPage();
        app.readCharacteristic(); //**new
    };

    ble.connect(deviceId, onConnect, app.onError);
},
readCharacteristic: function(event) {
   console.log("readCharacteristic");
    var deviceId = event.target.dataset.deviceId;
    ble.read(deviceId, 'FF10', 'FF11', app.onReadCharacteristsic, app.onError); //**new
},
onReadCharacteristsic: function(data) {
    console.log(data);

    //find length of data result //**new
    Object.size = function(obj) {
        var size = 0, key;
        for (key in obj) {
           if (obj.hasOwnProperty(key)) size++;
        }
        return size;
    };

    // Get the size of an object
    var size = Object.size(data);
    console.log(size);
    ////////////////////////////

    var a = new Uint8Array(data);
    batteryState.innerHTML = a[0];
},
disconnect: function(event) {
    var deviceId = event.target.dataset.deviceId;
    ble.disconnect(deviceId, app.showMainPage, app.onError);
},
showMainPage: function() {
    mainPage.hidden = false;
    detailPage.hidden = true;
},
showDetailPage: function() {
    mainPage.hidden = true;
    detailPage.hidden = false;
},
onError: function(reason) {
    alert("ERROR: " + reason); // real apps should use notification.alert
}
};

Peripheral code:

 // Smartbotic Service
var SERVICE_UUID = 'FF10';
var SWITCH_UUID = 'FF11';
var DIMMER_UUID = 'FF12';

var app = {
initialize: function() {
   this.bindEvents();
},
bindEvents: function() {
   document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {

   blePeripheral.onWriteRequest(app.didReceiveWriteRequest);
   blePeripheral.onBluetoothStateChange(app.onBluetoothStateChange);

   app.createServiceJSON();
},
createServiceJSON: function() {

   var property = blePeripheral.properties;
   var permission = blePeripheral.permissions;

   var flashlightService = {
   uuid: SERVICE_UUID,
   characteristics: [
                     {
                     uuid: SWITCH_UUID,
                     properties: property.WRITE | property.READ,
                     permissions: permission.WRITEABLE | permission.READABLE,
                     descriptors: [
                                   {
                                   uuid: '2901',
                                   value: 'Switch'
                                   }
                                   ]
                     },
                     {
                     uuid: DIMMER_UUID,
                     properties: property.WRITE | property.READ,
                     permissions: permission.WRITEABLE | permission.READABLE,
                     descriptors: [
                                   {
                                   uuid: '2901',
                                   value: 'Dimmer'
                                   }
                                   ]
                     }
                     ]
   };

   Promise.all([
                blePeripheral.createServiceFromJSON(flashlightService),
                blePeripheral.startAdvertising(flashlightService.uuid, 'Flashlight')
                ]).then(
                        function() { console.log ('Created Flashlight Service'); },
                        app.onError
                        );
},
didReceiveWriteRequest: function(request) {
   console.log(request);

   // Android sends long versions of the UUID
   if (request.characteristic === SWITCH_UUID || request.characteristic === '0000ff11-0000-1000-8000-00805f9b34fb') {
       var data = new Uint8Array(request.value);
       if (data[0] === 0) {
           window.plugins.flashlight.switchOff();
       } else {
           window.plugins.flashlight.switchOn();
       }
   }

   // brightness only works on iOS as of Flashlight 3.2.0
   if (request.characteristic === DIMMER_UUID || request.characteristic === '0000ff12-0000-1000-8000-00805f9b34fb') {
       var data = new Uint8Array(request.value);
       var brightnessByte = data[0];              // 1 byte value 0x00 to 0xFF
       var brightness = brightnessByte / 255.0    // convert to value between 0 and 1.0
       window.plugins.flashlight.switchOn(
                                          function() { console.log('Set brightness to', brightness) },
                                          function() { console.log('Set brightness failed')},
                                          { intensity: brightness }
                                          );
   }
},
onBluetoothStateChange: function(state) {
   console.log('Bluetooth State is', state);
  outputDiv.innerHTML += 'Bluetooth  is ' +  state + '<br/>';
}
};

app.initialize();
@nickcom
Copy link
Author

nickcom commented Jan 25, 2020

almost a year later... any ideas on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant