2016-12-01 223 views
0

我有一個醫療設備(命名爲SnoreCoach),我創建了一個Android應用程序,並使用此設備進行了藍牙低功耗連接。除了OnCharacteristicChanged沒有被觸發,所有東西(連接到設備,寫入數據到特徵等)都工作正常。Android BLE onCharacteristicChanged沒有觸發

以下是我BluetoothGattCallback:

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { 
    @Override 
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 
     super.onConnectionStateChange(gatt, status, newState); 
     if (newState == BluetoothProfile.STATE_CONNECTED) { 
      broadcastUpdate(ACTION_GATT_CONNECTED); 
     } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { 
      broadcastUpdate(ACTION_GATT_DISCONNECTED); 
     } 

    } 

    @Override 
    public void onServicesDiscovered(BluetoothGatt gatt, int status) { 
     super.onServicesDiscovered(gatt, status); 
     if (status == BluetoothGatt.GATT_SUCCESS) { 
      Log.d(TAG, "onServicesDiscovered - success"); 
      broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED); 
     } 
    } 

@Override 
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
     super.onCharacteristicRead(gatt, characteristic, status); 
     if (status == BluetoothGatt.GATT_SUCCESS) { 
      Log.d(TAG, "onCharacteristicRead - success"); 
      broadcastUpdate(ACTION_DATA_AVAILABLE); 
     } 
    } 

    @Override 
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
     super.onCharacteristicWrite(gatt, characteristic, status); 
     if (status == BluetoothGatt.GATT_SUCCESS) { 
      Log.d(TAG, "onCharacteristicWrite - success"); 
      if (writeType.equals("unPair")) { 
       mBluetoothGatt.close(); 
       mBluetoothGatt = null; 
       currentlyConnectedPeripheral = null; 
      } 
     } 
    } 

    @Override 
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { 
     super.onCharacteristicChanged(gatt, characteristic); 
     Log.d(TAG, "onCharacteristicChanged"); 
     broadcastUpdate(ACTION_DATA_AVAILABLE); 
    } 

    @Override 
    public void onReliableWriteCompleted(BluetoothGatt gatt, int status) { 
     super.onReliableWriteCompleted(gatt, status); 
     Log.d(TAG, "onReliableWriteCompleted"); 
    } 

    @Override 
    public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) { 
     super.onReadRemoteRssi(gatt, rssi, status); 
     Log.d(TAG, "onReadRemoteRssi"); 
    } 

    @Override 
    public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) { 
     super.onMtuChanged(gatt, mtu, status); 
     Log.d(TAG, "onMtuChanged"); 
    } 
}; 

以下爲 「broadcastUpdate」 功能從上面BluetoothGattCallback稱爲:

private void broadcastUpdate(final String action) { 
    final Intent intent = new Intent(action); 
    sendBroadcast(intent); 
} 

在我的呼喚活動(SettingsActivity)我有以下廣播接收器:

private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     final String action = intent.getAction(); 
     switch (action) { 
      case BLEService.ACTION_GATT_CONNECTED: 
       displayToast("Connected"); 
       Log.d(TAG, "Connected"); 
       mBleService.discoverServices(); 
       break; 
      case BLEService.ACTION_GATT_DISCONNECTED: 
       displayToast("Disconnected"); 
       break; 
      case BLEService.ACTION_GATT_SERVICES_DISCOVERED: 
       Log.d(TAG, "Services Discovered"); 
       displayToast("Services Discovered"); 
       mBleService.enableNotification(); 
       break; 
      case BLEService.ACTION_DATA_AVAILABLE: 
       displayToast("Data Received"); 
       break; 
     } 
    } 
}; 

而下面是我的「enableNotification」func蒸發散:

public void enableNotification() { 
    BluetoothGattService snorecoachService = mBluetoothGatt.getService(SNORECOACH_SERVICE_UUID); 
    if (snorecoachService == null) { 
     Log.d(TAG, "snorecoach Service not found!"); 
     return; 
    } 

    BluetoothGattCharacteristic bodyMovementChar = snorecoachService.getCharacteristic(BODY_MOVEMENT_UUID); 
    if (bodyMovementChar == null) { 
     Log.d(TAG, "charateristic not found!"); 
     return; 
    } 

    mBluetoothGatt.setCharacteristicNotification(bodyMovementChar, true); 

    BluetoothGattDescriptor desc = bodyMovementChar.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID); 
    desc.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); 
    mBluetoothGatt.writeDescriptor(desc); 
} 

以下是流程:

1)在連接時,我發送一個廣播的「連接」到mGattUpdateReceive,在這裏我再啓動服務發現。 2)OnServiceDiscovery,我發送一個廣播作爲「服務發現」到mGattUpdateReceive,在這裏我稱之爲「啓用notofication」函數來設置「setCharacteristicNotification」,它也寫入所需的描述符。

我嘗試了所有可能的選項,我從其他stackoverflow問題找到,但我不知道我做錯了onCharacteristic事件沒有得到觸發。

我已經花了超過2天解決這個問題,但沒有運氣,所以任何幫助將不勝感激。

+0

我面臨着同樣的問題,但僅適用於Android 7.1.1 ...是你能夠得到它的工作? – AndroidMechanic

+0

是的,就我而言,問題出在設備上,我不得不在設備上編寫一個特性,並且它開始正常工作。 –

回答

1

如果你想,而不是指示通知,試圖改變

desc.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); 

desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 
+0

我嘗試了兩種選擇,但沒有運氣。 –

+0

然後這是外圍的問題,而不是與Android ... – Emil

+0

但同樣的外設工作正常與iOS應用程序 –