hansemannn / titanium-bluetooth

📡 Native iOS / Android Bluetooth support for the Axway Titanium SDK
Other
45 stars 22 forks source link

Example how to setup #28

Open focussing opened 7 years ago

focussing commented 7 years ago

I am struggling to implement the following

Next I want to get the UUIDs from the services, and subscribe to let's say one of them My code so far:

    centralManager.addEventListener('didConnectPeripheral', function(e) {
        console.log('\ndidConnectPeripheral');
        console.log(e);
        console.log(e.peripheral);

        centralManager.stopScan();
        connectedPeripheral = e.peripheral;

        connectedPeripheral.addEventListener('didDiscoverServices', function(e) {
            console.log('\ndidDiscoverServices');
            console.log(e);
            console.log(e.peripheral);
            console.log(e.peripheral.services);

            console.log('peripheral has ', e.peripheral.services.length, 'service(s)');
            var services = e.peripheral.services;

            for (var i = 0; i < services.length; i++)
                console.log('service characteristics ', services[i].characteristics.uuid);
            // if (service.UUID.toLowerCase() == CUSTOM_SERVICE_UUID.toLowerCase()) {
            // e.peripheral.discoverCharacteristicsForService(service);
            // }
        });

        connectedPeripheral.addEventListener('didDiscoverCharacteristicsForService', function(e) {
            console.log('didDiscoverCharacteristicsForService');
            console.log(e);
        });

        connectedPeripheral.addEventListener('didUpdateValueForCharacteristic', function(e) {
            console.log('didUpdateValueForCharacteristic');
            console.log(e);
        });

        connectedPeripheral.discoverServices();
    });
wsliaw commented 7 years ago

You have to add the event listener on the latest peripheral you get

centralManager.addEventListener('didConnectPeripheral', function(e) {
    console.log('\ndidConnectPeripheral');
    console.log(e);
    console.log(e.peripheral);

    centralManager.stopScan();
    connectedPeripheral = e.peripheral;

    connectedPeripheral.addEventListener('didDiscoverServices', function(e) {
        console.log('\ndidDiscoverServices');
        console.log(e);
        console.log(e.peripheral);
        console.log(e.peripheral.services);

        var peripheral = e.peripheral;

        peripheral.addEventListener('didDiscoverCharacteristicsForService', function(e) {
            console.log('didDiscoverCharacteristicsForService');
            console.log(e);
        });

        peripheral.services.forEach(function(service) {
            peripheral.discoverCharacteristicsForService({ service: service });
        });
    });

    connectedPeripheral.discoverServices();
});
focussing commented 7 years ago

I am doing that, please take a look at https://github.com/hansemannn/titanium-bluetooth/issues/23

I updated that issue today.