75

テストに使用したデバイス: Nexus 4、Android 4.3

接続は正常に機能onCharacteristicChangedしていますが、コールバックのメソッドが呼び出されません。setCharacteristicNotification(char, true)ただし、内部を使用して通知を登録しonServicesDiscoveredていますが、その関数は true を返します。

デバイス ログ (実際には、通知が表示されるべきか、Bluetooth デバイス経由で送信されるときに、メッセージはまったくありません):

07-28 18:15:06.936  16777-16809/de.ffuf.leica.sketch D/BluetoothGatt: setCharacteristicNotification() - uuid: 3ab10101-f831-4395-b29d-570977d5bf94 enable: true
07-28 18:15:06.936    4372-7645/com.android.bluetooth D/BtGatt.GattService: registerForNotification() - address=C9:79:25:34:19:6C enable: true
07-28 18:15:06.936    4372-7645/com.android.bluetooth D/BtGatt.btif: btif_gattc_reg_for_notification
07-28 18:15:06.946    4372-7645/com.android.bluetooth D/BtGatt.btif: btgattc_handle_event: Event 1018
07-28 18:15:06.946    4372-7645/com.android.bluetooth D/BtGatt.GattService: onRegisterForNotifications() - address=null, status=0, registered=1, charUuid=3ab10101-f831-4395-b29d-570977d5bf94
07-28 18:15:06.946    4372-7645/com.android.bluetooth D/BtGatt.btif: btgattc_handle_event: Event 1016
07-28 18:15:06.946    4372-7645/com.android.bluetooth D/BtGatt.btif: btgattc_handle_event: Event 1018
07-28 18:15:06.946    4372-7645/com.android.bluetooth D/BtGatt.GattService: onRegisterForNotifications() - address=null, status=0, registered=1, charUuid=3ab10102-f831-4395-b29d-570977d5bf94
07-28 18:15:06.946    4372-7645/com.android.bluetooth D/BtGatt.btif: btgattc_handle_event: Event 1016
07-28 18:15:06.946    4372-7684/com.android.bluetooth E/bt-btif: already has a pending command!!
07-28 18:15:06.946    4372-7645/com.android.bluetooth D/BtGatt.btif: btgattc_handle_event: Event 1013
07-28 18:15:06.946    4372-7684/com.android.bluetooth E/bt-btif: already has a pending command!!
07-28 18:15:06.946    4372-7645/com.android.bluetooth D/BtGatt.btif: btgattc_handle_event: Event 1013
07-28 18:15:06.946    4372-7684/com.android.bluetooth E/bt-btif: already has a pending command!!
07-28 18:15:06.976    4372-7645/com.android.bluetooth D/BtGatt.btif: btif_gattc_upstreams_evt: Event 9

GATT 通知は iOS を使用して正常に動作し、アプリは基本的に Android と同じように動作します (通知の登録など)。

他の誰かが可能な解決策でこれを経験しましたか?

4

10 に答える 10

86

BLE デバイスにこのモードにするよう指示する Descriptor を書き忘れていたようです。http://developer.android.com/guide/topics/connectivity/bluetooth-le.html#notificationで記述子を扱うコード行を参照してください。

この記述子を設定しないと、特性の更新を受け取ることはありません。電話をかけるsetCharacteristicNotificationだけでは不十分です。これはよくある間違いです。

コードが切り取られました

protected static final UUID CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");

public boolean setCharacteristicNotification(BluetoothDevice device, UUID serviceUuid, UUID characteristicUuid,
        boolean enable) {
    if (IS_DEBUG)
        Log.d(TAG, "setCharacteristicNotification(device=" + device.getName() + device.getAddress() + ", UUID="
                + characteristicUuid + ", enable=" + enable + " )");
    BluetoothGatt gatt = mGattInstances.get(device.getAddress()); //I just hold the gatt instances I got from connect in this HashMap
    BluetoothGattCharacteristic characteristic = gatt.getService(serviceUuid).getCharacteristic(characteristicUuid);
    gatt.setCharacteristicNotification(characteristic, enable);
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID);
    descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[] { 0x00, 0x00 });
    return gatt.writeDescriptor(descriptor); //descriptor write operation successfully started? 
}
于 2013-08-02T08:11:45.940 に答える
7

Googleが望んでいたように実装しなかったと思います(ソースコードを提供していません):

(1)

mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

その後

(2)

BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);

2が欠けていると思います。その場合、低レベルの通知がトリガーされると信じていますが、アプリケーション層に報告されることはありません。

于 2016-07-12T12:43:38.907 に答える
1

これは私のために働いています:

いくつかの特性が変化したことをマスター デバイスに通知するには、周辺機器でこの関数を呼び出します。

private BluetoothGattServer server;
//init....

//on BluetoothGattServerCallback...

//call this after change the characteristic
server.notifyCharacteristicChanged(device, characteristic, false);

マスター デバイスで: サービスを検出した後、setCharacteristicNotification を有効にします。

@Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        super.onServicesDiscovered(gatt, status);
        services = mGatt.getServices();
        for(BluetoothGattService service : services){
            if( service.getUuid().equals(SERVICE_UUID)) {
                characteristicData = service.getCharacteristic(CHAR_UUID);
                for (BluetoothGattDescriptor descriptor : characteristicData.getDescriptors()) {
                    descriptor.setValue( BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                    mGatt.writeDescriptor(descriptor);
                }
                gatt.setCharacteristicNotification(characteristicData, true);
            }
        }
        if (dialog.isShowing()){
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    dialog.hide();
                }
            });
        }
   }

onCharacteristicRead関数など、特性値が変更されていることを確認できるようになりました(これはonCharacteristicChanged関数でも機能します):

@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        Log.i("onCharacteristicRead", characteristic.toString());
        byte[] value=characteristic.getValue();
        String v = new String(value);
        Log.i("onCharacteristicRead", "Value: " + v);
}
于 2016-12-18T11:28:52.420 に答える
-1

AndroidでもBLEの通知で問題が発生しました。ただし、Bluetooth ラッパーを含む完全に機能するデモがありますBluetoothAdapter。ラッパーが呼び出され、 Application Acceleratorパッケージに含まれるBLEDemoBleWrapperというデモ アプリケーションとともに出荷されます。ここからダウンロード: https://developer.bluetooth.org/Pages/Bluetooth-Android-Developers.aspx . ダウンロードする前に、右上のメールアドレスを登録する必要があります。プロジェクトのライセンスでは、自由な使用、コードの変更、公開が許可されています。

私の経験では、Android デモ アプリケーションは BLE 通知サブスクリプションを非常にうまく処理します。ラッパーが実際にどのようにラップするかを確認するために、まだコードを深く掘り下げていません。

アプリケーション アクセラレータのデモをカスタマイズした Android アプリを Play ストアで入手できます。ユーザー インターフェイスはほぼ同じに見えるので、BleWrapper. アプリのダウンロードはこちら: https://play.google.com/store/apps/details?id=com.macdom.ble.blescanner

于 2015-08-05T10:04:26.953 に答える