Subscribe to ANCS on Android
Asked Answered
A

2

6

Based on the services listed here, I'm trying to subscribe to a service from an Android device, but can't get the solicitation part working. Tried to advertise using 7905F431-B5CE-4E99-A40F-4B1E122D00D0 but the peripheral is not showing up on the iPhone's bluetooth devices list. Also played with (LE)scan and filters to discover the ANCS server without luck. Any hint?

edit: code

BleService.java

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                          boolean enabled) {
    Log.d(TAG, "setCharacteristicNotification");
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.DESCRIPTOR));
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor)
}

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
    }
}

SampleGattAttributes.java

public class SampleGattAttributes {
    public static String ANCS_SERVICE = "7905F431-B5CE-4E99-A40F-4B1E122D00D0";
    public static String NOTIFICATION_SOURCE = "9FBF120D-6301-42D9-8C58-25E699A21DBD";
    public static String DATA_CONTROL = "22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB";
    public static String DESCRIPTOR = "00002902-0000-1000-8000-00805f9b34fb";
}

MainActivity.java

private void displayServices(List<BluetoothGattService> services) {
    for (BluetoothGattService service : services) {
        if (service.getUuid().compareTo(UUID.fromString(ANCS_SERVICE)) == 0) {
            for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
                Log.d(TAG, "charac: " + characteristic.getUuid());
                for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
                    Log.d(TAG, "desc: " + descriptor.getUuid());
                }
                if (characteristic.getUuid().compareTo(UUID.fromString(NOTIFICATION_SOURCE)) == 0) {
                    bleService.setCharacteristicNotification(characteristic, true);
                }
            }
        }
    }
}

private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (BleService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
            displayServices(bleService.getSupportedGattServices());
        }
    }
};

update: I'm able to find the services and characteristics after connecting to a service advertised on the iPhone. However, subscribing to notifications doesn't trigger onCharacteristicChanged upon receiving a notification on the iPhone.

update2: all the write descriptor.setValue calls are successful, and run in sequence.

update3: used part of the code from this sample.

update4: test devices: Nexus 5X Android 6.0.1; iPhone 6S+ iOS 10.3.1

update5: BT logs

write request enter image description here

write response enter image description here

Aramen answered 18/4, 2017 at 0:11 Comment(3)
well In Nexus devices go On the developer options, there is one checkbox which is "Enable Bluetooth HCI snoop log". When you enable it, the device will log everything that's happening on the Bluetooth stack....see log stored in Sd card called "HCIsnoop.log". When you have your file on your HD, download this s/w (free): fte.com/support/… to view these BT log file. With this u can see any packet that has been exchanged from one device to another (like a Wireshark for BT).Bipartisan
Any way you can post this application on Github? A simple ANCS Android client would be amazingRoscoe
Was you able to get notifications from iOS on android?Gualtiero
C
3

Oh I had a similar issue, make sure you don't use Local Notifications when testing on iOS as those won't be propagated through ANCS. You need real push notifications for whatever reason.

Crayton answered 21/4, 2017 at 22:2 Comment(0)
P
0

I encountered a similar issue recently. What I ended up doing to resolve it was adding a writeDescriptor to my BluetoothGatt. My setCharacteristicNotification now looks like this:

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
    Log.w(TAG, "BluetoothAdapter not initialized");
    return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
    UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}

Perhaps if you could post/link your code I would be able to further diagnose your exact issue, but the above code was the solution to my problem.

Proportionable answered 20/4, 2017 at 3:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.