Hi aca-mobile,
I have tried to implement code for android device with new version of this library. didDiscoverPeripheral find my peripheral (in my sample UART Service);
didConnectPeripheral run correctly;
didUpdateValueForCharacteristic don't run correctly:
This is my log:
[INFO] : didDiscoverPeripheral
[INFO] : UART Service
[INFO] : Connect to [object TiBluetoothPeripheral]
[INFO] : e.peripheral: [object TiBluetoothPeripheral]
[INFO] : e.peripheral.name: UART Service
[INFO] : connecting
[INFO] : Connected to Periperal...
[INFO] : Service Discovery...
[INFO] : peripheral has 3 service(s)
[INFO] : service: 00001801-0000-1000-8000-00805F9B34FB
[INFO] : service: 00001800-0000-1000-8000-00805F9B34FB
[INFO] : service: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
[INFO] : service Found: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
[INFO] : Service Found: 00001801-0000-1000-8000-00805F9B34FB
[INFO] : Service Found: 00001800-0000-1000-8000-00805F9B34FB
[INFO] : Service Found: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
[INFO] : Immediate Alert Service Found6E400001-B5A3-F393-E0A9-E50E24DCCA9E
[INFO] : Characteristic ID: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E
[INFO] : Alert Level Characteristic Found
[INFO] : 6E400003-B5A3-F393-E0A9-E50E24DCCA9E
[INFO] : didUpdateValueForCharacteristic passed -->6E400003-B5A3-F393-E0A9-E50E24DCCA9E
[INFO] : Characteristic ID: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E
[INFO] : Characteristic ID: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E
[INFO] : Alert Level Characteristic Found
[INFO] : 6E400003-B5A3-F393-E0A9-E50E24DCCA9E
[INFO] : didUpdateValueForCharacteristic passed -->6E400003-B5A3-F393-E0A9-E50E24DCCA9E
[INFO] : Characteristic ID: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E
And this is my sample code:
var BLE = require('ti.bluetooth');
var centralManager = BLE.createCentralManager();
var value = '';
var connectedPeripheral;
var SERVICE_UUID = '6E400001-B5A3-F393-E0A9-E50E24DCCA9E';
var CHARACTERISTIC_UUID_RX = '6E400002-B5A3-F393-E0A9-E50E24DCCA9E';
var CHARACTERISTIC_UUID_TX = '6E400003-B5A3-F393-E0A9-E50E24DCCA9E';
var win = Ti.UI.createWindow({
backgroundColor : '#fff'
});
var btn1 = Ti.UI.createButton({
title : 'Start scan',
top : 40
});
btn1.addEventListener('click', function() {
if (centralManager.isScanning()) {
alert('Already scanning, please stop scan first!');
return;
} else if (centralManager.getState() != BLE.MANAGER_STATE_POWERED_ON) {
alert('The BLE manager needs to be powered on before. Call initialize().');
return;
}
centralManager.startScan();
});
var btn2 = Ti.UI.createButton({
title : 'Stop scan',
top : 100
});
btn2.addEventListener('click', function() {
if (!centralManager.isScanning()) {
alert('Not scanning!');
return;
}
centralManager.stopScan();
});
/**
- Central Manager Events
*/
centralManager.addEventListener('didDiscoverPeripheral', function(e) {
Ti.API.info('didDiscoverPeripheral');
Ti.API.info(e.peripheral.name);
Ti.API.info('Connect to ' + e.peripheral);
if (e.peripheral.name == 'UART Service') {
Ti.API.info('e.peripheral:', e.peripheral);
Ti.API.info('e.peripheral.name:', e.peripheral.name);
Ti.API.info('connecting');
centralManager.stopScan();
centralManager.connectPeripheral(e.peripheral, {
notifyOnConnection : true,
notifyOnDisconnection : true
});
}
});
centralManager.addEventListener('didUpdateState', function(e) {
Ti.API.info('didUpdateState');
switch (e.state) {
case BLE.MANAGER_STATE_RESETTING:
Ti.API.info('Resetting');
break;
case BLE.MANAGER_STATE_UNSUPPORTED:
Ti.API.info('Unsupported');
break;
case BLE.MANAGER_STATE_UNAUTHORIZED:
Ti.API.info('Unauthorized');
break;
case BLE.MANAGER_STATE_POWERED_OFF:
Ti.API.info('Powered Off');
break;
case BLE.MANAGER_STATE_POWERED_ON:
Ti.API.info('Powered On');
break;
case BLE.MANAGER_STATE_UNKNOWN:
default:
Ti.API.info('Unknown');
break;
}
});
centralManager.addEventListener('didConnectPeripheral', function(e) {
Ti.API.info('Connected to Periperal...');
connectedPeripheral = e.peripheral;
connectedPeripheral.addEventListener('didDiscoverServices', function(e) {
Ti.API.info('Service Discovery...');
p = e.peripheral;
Ti.API.info('peripheral has', p.services.length, 'service(s)');
p.services.forEach(function(service) {
// Assign service
Ti.API.info("service: " + service.uuid);
if (service.uuid == SERVICE_UUID) {
Ti.API.info("service Found: " + service.uuid);
p.addEventListener('didDiscoverCharacteristicsForService', function(e) {
var peripheral = e.peripheral;
var characteristics = e.service.characteristics;
characteristics.forEach(function(characteristic) {
Ti.API.info('Characteristic ID:', characteristic.uuid);
if (characteristic.uuid == CHARACTERISTIC_UUID_TX) {
Ti.API.info('Alert Level Characteristic Found');
Ti.API.info(characteristic.uuid);
peripheral.addEventListener('didUpdateValueForCharacteristic', _didUpdateValueForCharacteristic);
peripheral.setNotifyValueForCharacteristic(true, characteristic);
Ti.API.info('didUpdateValueForCharacteristic passed -->' + characteristic.uuid);
function _didUpdateValueForCharacteristic(e) {
var characteristic = e.characteristic.value.toString().replace(/(\r\n|\n|\r)/gm, "");
value = value + characteristic;
Ti.API.info(' data:' + characteristic + ' length:' + characteristic.length);
}
}
});
});
p.services.forEach(function(service) {
Ti.API.info("Service Found: " + service.uuid);
if (service.uuid == SERVICE_UUID) {
Ti.API.info('Immediate Alert Service Found' + service.uuid);
p.discoverCharacteristicsForService({
characteristics : [],
service : service
});
}
});
p.discoverCharacteristicsForService({
characteristics : [],
service : service
});
}
});
});
// Discover services
connectedPeripheral.discoverServices();
});
centralManager.addEventListener('didDisconnectPeripheral', function(e) {
Ti.API.info('didDisconnectPeripheral');
Ti.API.info(e);
});
centralManager.addEventListener('willRestoreState', function(e) {
Ti.API.info('willRestoreState');
Ti.API.info(e);
});
centralManager.addEventListener('didFailToConnectPeripheral', function(e) {
Ti.API.info('didFailToConnectPeripheral');
Ti.API.info(e);
});
/**
- Peripheral Manager Events
*/
if (Ti.Platform.name === 'iPhone OS') {
peripheralManager.addEventListener('didUpdateState', function(e) {
Ti.API.info('didUpdateState');
Ti.API.info(e);
});
peripheralManager.addEventListener('willRestoreState', function(e) {
Ti.API.info('willRestoreState');
Ti.API.info(e);
});
peripheralManager.addEventListener('didStartAdvertising', function(e) {
Ti.API.info('didStartAdvertising');
Ti.API.info(e);
});
peripheralManager.addEventListener('didAddService', function(e) {
Ti.API.info('didAddService');
Ti.API.info(e);
});
peripheralManager.addEventListener('didSubscribeToCharacteristic', function(e) {
Ti.API.info('didSubscribeToCharacteristic');
Ti.API.info(e);
});
peripheralManager.addEventListener('didUnsubscribeFromCharacteristic', function(e) {
Ti.API.info('didUnsubscribeFromCharacteristic');
Ti.API.info(e);
});
peripheralManager.addEventListener('didReceiveReadRequest', function(e) {
Ti.API.info('didReceiveReadRequest');
Ti.API.info(e);
});
peripheralManager.addEventListener('didReceiveWriteRequests', function(e) {
Ti.API.info('didReceiveWriteRequests');
Ti.API.info(e);
});
peripheralManager.addEventListener('readyToUpdateSubscribers', function(e) {
Ti.API.info('readyToUpdateSubscribers');
Ti.API.info(e);
});
}
win.add(btn1);
win.add(btn2);
win.open();
Whats's wrong in my code?
The same code in IOS work fine!
Best regards,
Michele.