tidev / appcelerator.ble

A collection of API's to connect and communicate with BLE compatible devices via Bluetooth LE
https://titaniumsdk.com/api/modules/ble.html
Other
7 stars 3 forks source link

peripheral.writeValueForCharacteristic not working? #348

Closed focussing closed 1 year ago

focussing commented 1 year ago

I have implemented your example application on iOS but I cannot get the peripheral.writeValueForCharacteristic function to work. My system is an Arduino Nano RP2040 connect, and I can successfully connect to it; and receive data from it. However sending data back is not working. The same situation occurs if I use an existing app which uses the Logical Labs Bluetooth Low Energy Module for iOS, it connects, receives data from the Arduino but sending data back is not working.

    writeValue.addEventListener('click', function () {
        if (peripheral.isConnected) {
            if (global.charactersticObject) {
                var data = valueField.value === '' || valueField.value === null ? 'temp data' : valueField.value;
console.log('data', data);
                var buffer = Ti.createBuffer({ value: data });
console.log('buffer', buffer);
                // Characteristic needs to have write permission & property
                peripheral.writeValueForCharacteristic({
                    data: buffer,
                    characteristic: global.charactersticObject,
                    type: BLE.CHARACTERISTIC_TYPE_WRITE_WITHOUT_RESPONSE
                });
            } else {
                alert('No heart rate characteristic (2A37) available to write value');
            }
        } else {
            alert('Peripheral is not connected.');
        }
    });

If I do console.log('buffer', buffer);I see {} in the terminal.

I tested it with 2 different iPhone SE phones (one on iOS 16.2 and one on 16.1.1). Previous test I am not sure, but I think it was on iOS15 and earlier.

When I use an iOS app like "BluetoothLE" sending and receiving data works perfectly...

Any idea? I hope you can help me (again) :)

focussing commented 1 year ago

https://nl.mathworks.com/matlabcentral/answers/556798-ble-write-to-arduino-nano-33-ble-is-unsuccessful

Searched on this page for BLEWriteWithoutResponse and found:

There is also another property 'BLEWriteWithoutResponse', which hasn't been discussed a lot on forums. And it is this property that needs to used in your Arduino Nano33 BLE sketch.

Now I can receive data... Next is to re-arrange my app back with both sending and receiving in parallel...

focussing commented 1 year ago

Working perfectly