Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subscribe to ANCS on Android

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

like image 576
mbmc Avatar asked Oct 18 '22 14:10

mbmc


1 Answers

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.

like image 96
Keith Avatar answered Oct 21 '22 11:10

Keith