JSON error notification/writeCharacteristic
Posted: 10:31, 19 Mar 2015
Hi,
i'm trying to make a simple app to play with my nrf51 dongle but i have a "JSON Error" when i do evothings.ble.enableNotification and evothings.ble.writeCharacteristic
Ty for your help, and nice job with this library
i'm trying to make a simple app to play with my nrf51 dongle but i have a "JSON Error" when i do evothings.ble.enableNotification and evothings.ble.writeCharacteristic
Ty for your help, and nice job with this library
Code: Select all
// Application object.
var app = {};
app.led="off";
app.UUID="6E400001-B5A3-F393-E0A9-E50E24DCCA9E"; //nordic uart uuid
app.txCharacteristic="6E400002-B5A3-F393-E0A9-E50E24DCCA9E"; // transmit is from the phone's perspective
app.rxCharacteristic="6E400003-B5A3-F393-E0A9-E50E24DCCA9E"; // receive is from the phone's perspective
app.deviceHandle=null;
// Device list.
app.devices = {};
// UI methods.
app.ui = {};
app.initialize = function()
{
document.addEventListener('deviceready', this.onDeviceReady, false);
// Important to stop scanning when page reloads/closes!
window.addEventListener('beforeunload', function(e)
{
app.stopScan();
});
};
//connect to device
app.connectTo = function(address)
{
function onConnectSuccess(info)
{
app.ui.hideDeviceList();
if (info.state==2)
{
app.device = info;
//TODO
app.getServices(info);
//display buttons
app.ui.displayStatus('Connected');
app.ui.displayCommands();
}
else if (info.state == 0)
{
app.ui.displayStatus('Disconnected');
}
}
function onConnectFailure(errorCode)
{
app.ui.displayStatus('Error connect '+errorCode);
//TODO
//app.disconnect('Deconnecté');
}
app.stopScan();
app.ui.displayStatus('Connecting');
evothings.ble.connect(address, onConnectSuccess, onConnectFailure);
};
//get services
app.getServices = function(device)
{
function onReadServicesSuccess(services)
{
for (var si in services)
{
var service = services[si];
for (var ci in service.characteristics)
{
var characteristic = service.characteristics[ci];
if (characteristic.uuid == app.rxCharacteristic)
{
app.characteristicRead = characteristic.handle;
}
else if (characteristic.uuid == app.txCharacteristic)
{
app.characteristicWrite = characteristic.handle;
}
}*/
}
}
function onNotifySuccess(data)
{
alert("data : "+data);
}
function onNotityFailure(errorCode)
{
alert("error notify "+errorCode);
}
//on s'inscrit pour recevoir les notifications
app.deviceHandle=device.handle;
evothings.ble.enableNotification(app.deviceHandle,app.rxCharacteristic,onNotifySuccess,onNotityFailure);
app.ui.displayStatus('Connected');
app.ui.displayCommands();
}
function onReadServicesFailure(errorCode)
{
app.ui.displayStatus('Error readService '+errorCode);
//TODO
}
app.ui.displayStatus('Reading services');
evothings.ble.readAllServiceData(device.deviceHandle,onReadServicesSuccess,onReadServicesFailure);
};
app.onDeviceReady = function()
{
// Not used.
// Here you can update the UI to say that
// the device (the phone/tablet) is ready
// to use BLE and other Cordova functions.
alert("ready");
};
// Start the scan. Call the callback function when a device is found.
// Format:
// callbackFun(deviceInfo, errorCode)
// deviceInfo: address, rssi, name
// errorCode: String
app.startScan = function(callbackFun)
{
app.stopScan();
console.log("starting");
evothings.ble.startScan(
function(device)
{
// Report success.
callbackFun(device, null);
},
function(errorCode)
{
// Report error.
callbackFun(null, errorCode);
}
);
};
// Stop scanning for devices.
app.stopScan = function()
{
evothings.ble.stopScan();
};
app.changeLed = function(etat)
{
alert(etat);
if (etat=="on")
{
alert("ledOn");
}
else if (etat=="off")
{
alert("ledOff");
}
};
app.ui.displayCommands = function()
{
$('#commandes').show();
};
app.ui.checkButtonToggeledLed = function ()
{
function onWriteSuccess()
{
alert("write ok");
}
function onWriteFailure(errorCode)
{
alert('error write '+errorCode);
}
function writeMsg(message)
{
alert("write");
var data = new Uint8Array(message.length);
for (var i = 0, messageLength = message.length;
i < messageLength;
i++)
{
data[i] = message.charCodeAt(i);
}
evothings.ble.writeCharacteristic(app.deviceHandle,app.txCharacteristic,data,onWriteSuccess,onWriteFailure);
}
if (app.led=="off")
{
app.led="on";
writeMsg('ledOn');
}
else if (app.led="on")
{
app.led="off";
writeMsg('ledOff');
}
};
// Called when Start Scan button is selected.
app.ui.onStartScanButton = function()
{
app.startScan(app.ui.deviceFound);
app.ui.displayStatus('Scanning...');
};
// Called when Stop Scan button is selected.
app.ui.onStopScanButton = function()
{
app.stopScan();
app.devices = {};
app.ui.displayStatus('Scanning turned off');
app.ui.displayDeviceList();
};
// Called when a device is found.
app.ui.deviceFound = function(device, errorCode)
{
if (device)
{
// Insert the device into table of found devices.
app.devices[device.address] = device;
// Display device in UI.
app.ui.displayDeviceList();
}
else if (errorCode)
{
app.ui.displayStatus('Scan Error: ' + errorCode);
}
};
// Display the device list.
app.ui.hideDeviceList = function()
{
// Clear device list.
$('#foundDevices').empty();
}
// Display the device list.
app.ui.displayDeviceList = function()
{
// Clear device list.
$('#foundDevices').empty();
var i = 1;
$.each(app.devices, function(key, device)
{
// Set background color for this item.
// Create a div tag to display sensor data.
var element = $(
'<li class="topcoat-list__item">'
//+ '<b>' + device.name + '</b><br /><button id=connect>Connect</button><br />'
+ '<b>' + device.name + '</b><br /><button id=connect onclick="app.connectTo(\''+device.address+'\')">Connect</button><br />'
// + device.address + '<br/>'
// + device.rssi + '<br/>'
+ '</li>'
);
$('#foundDevices').append(element);
});
};
// Display a status message
app.ui.displayStatus = function(message)
{
$('#scan-status').html(message);
};
app.initialize();