2016-11-15 6 views
0

Bluetoothscannerを構築しようとしています。 しかし、startDiscovery()は起動していません。私は既にパーミッションを含んでいます。Android 7.0 Bluetoothの検出が開始されない

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
<uses-permission android:name="android.permission.BLUETOOTH" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

私のコードは次のとおりです。

final BluetoothManager mBluetoothManager = 
(BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE); 
mBluetoothAdapter = mBluetoothManager.getAdapter(); 
mBluetoothAdapter.disable(); 
onoff.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
      if (!mBluetoothAdapter.isEnabled()) { 
       mBluetoothAdapter.enable(); 
       onoff.setChecked(true); 
      } 
      else { 
       mBluetoothAdapter.disable(); 
       onoff.setChecked(false); 
      } 
      if(!mBluetoothAdapter.isDiscovering()){ 
       Log.e("Searching?: ", "is not searching"); 
       mBluetoothAdapter.startDiscovery(); 
       Log.d("SearchingStatus: ", "startDiscovery() started!"); 
       Log.e("Searching?", String.valueOf(mBluetoothAdapter.isDiscovering())); 
      } 
     } 
    }); 

私の最後のログは次のようになります。

Searching?: false 

答えて

0

BluetoothAdapter.enableは、非同期呼び出しです:それはすぐに返され、クライアントはACTION_STATE_CHANGEDを​​リッスンその後のアダプター状態の変更を通知するには、https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#enable()

BluetoothAdapter.enable/BluetoothAdapter.disableへのすべての呼び出しをコメントにして、アプリケーションを起動する前にBluetoothが有効になっていることを確認してください(設定 - > Bluetooth)。

私はmBluetoothAdapter.startDiscovery()の戻り値をチェックし、falseを返すとLog.eを持ちます。

関連する問題