0

我註冊了ACTION_BOND_STATE_CHANGED的藍牙,並且當我運行該應用程序時,switch-case語句中的非情況執行,而我收到的 是一個整數值,如下所示我不知道它的意義。意圖返回的整數沒有對應的常量值

我怎麼解釋它?

代碼

int currBondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR); 
      Log.d(TAG, LogAnd.show("onReceive", "currBondState: "+currBondState)); 
      switch (currBondState) { 
      case BluetoothDevice.BOND_BONDING: 
       Log.d(TAG, LogAnd.show("onReceive", "currBondState: BOND_BONDING")); 
       tvStatus.setText("currBondState:BOND_BONDING"); 
       break; 
      case BluetoothDevice.BOND_BONDED: 
       Log.d(TAG, LogAnd.show("onReceive", "currBondState: BOND_BONDED")); 
       tvStatus.setText("currBondState:BOND_BONDED"); 
       break; 
      case BluetoothDevice.BOND_NONE: 
       Log.d(TAG, LogAnd.show("onReceive", "currBondState: BOND_NONE")); 
       tvStatus.setText("currBondState:BOND_NONE"); 
       break; 
      } 

08-25 17:16:06.803: D/MainActivity(22326): -> onReceive:currBondState: -2147483648 
08-25 17:16:06.803: D/MainActivity(22326): -> onReceive:prevBondState: -2147483648 

回答

1

你所得到的值是恆定的ERRORerror。你也應該處理這個案例。
由於有幾個可選的返回值,如DEVICE_TYPE_CLASSICDEVICE_TYPE_DUAL等,我建議你也處理它們。如果您對它們沒有興趣,則可以添加DEFAULT聲明。

0

intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);

此行將從意圖返回BluetoothDevice.EXTRA_BOND_STATE值或BluetoothDevice.ERROR如果值不存在

在你的情況下,返回的值是BluetoothDevice.ERROR,在你的switch聲明中沒有case

0

另外一定要將藍牙許可添加到您的清單。

+0

權限和我收到的價值之間的關係是什麼? – rmaik

+0

由於adelphus說您收到錯誤值,因爲getextra不存在。 Als檢查intent.getAction,如果您收到的確實是債券變更響應。 –