2017-11-14 26 views
1

Bluetoothをオン/オフするアプリケーションを作成しました。私はスイッチボタンを使いました。Bluetoothを有効/無効にする

public class MainActivity extends AppCompatActivity { 
private static final String TAG = "MainActivity"; 
private final static int REQUEST_ENABLE_BT = 1; 
BluetoothAdapter mBluetoothAdapter = null; 

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if (action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED)) { 
      final int state = intent.getIntExtra(mBluetoothAdapter.EXTRA_STATE, mBluetoothAdapter.ERROR); 

      switch (state){ 
       case BluetoothAdapter.STATE_OFF: 

        break; 
       case BluetoothAdapter.STATE_ON: 
        break; 
      } 
     } 
    } 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final Switch bluetoothSwitch = (Switch) findViewById(R.id.bluetoothSwitch); 

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if (mBluetoothAdapter == null){ 
     //Device does not support bluetooth 
    } 
    else{ 
     bluetoothSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
       if(bluetoothSwitch.isChecked() && !mBluetoothAdapter.isEnabled()){ 
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 

        IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); 
        registerReceiver(mReceiver, BTIntent); 
       } 
       if(!bluetoothSwitch.isChecked() && mBluetoothAdapter.isEnabled()){ 
        mBluetoothAdapter.disable(); 
        IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); 
        registerReceiver(mReceiver, BTIntent); 
       } 
      } 
     }); 
    } 
} 

}

問題は、私は私のアプリからの設定から、Bluetoothをオン/オフしていない場合は、スイッチが変化しないということである。ここでは、コードです。 ブロードキャストレシーバを実装しましたが、スイッチからアクセスできません。放送受信機内部の

bluetoothSwich.setChecked(true) 

が、それは動作しません: 私が試してみました。

のTy

EDIT: 私は部分的に私は私のアプリに少なくとも1時間からオン/オフボタンをクリックする必要が最初の設定からオン/オフ動作をキャッチするが、世界的なスイッチに問題を解決しました。なにか提案を?

+0

は、には、Androidマニフェストファイル内に受信機を追加していますか?広いキャストレシーバーの中でそれにアクセスするためにあなたのスイッチボタンをグローバルにしてください –

+0

私はアンドロイドマニフェストで受信機を追加しました。私はswtichボタンをグローバルにしようとしましたが、ブロードキャストレシーバーからアクセスできますが、設定からブルートゥースのオン/オフをトリガーすることはできません – Tenrampi

答えて

1

ブルートゥースの状態変化を検出するには、AndroidManifest.xmlに次の権限を追加する必要があります。

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

好ましくはローカルブロードキャストを使用します。マニフェストに登録する必要はありません。 。(必要な場合は、マニフェストに登録し、その後アプリを通じて)あなたがそれを必要とする、実行時にそれを登録

private final BroadcastReceiver bStateReceiver = new BroadcastReceiver() { 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { 
      if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) { 
       // Bluetooth is disconnected, do handling here 
      } 
     } 
    } 

}; 

ランタイム登録:

LocalBroadcastManager.getInstance(this).registerReceiver(bStateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)); 

ランタイム登録解除:ブロードキャストの登録を解除することを忘れないでくださいを。

LocalBroadcastManager.getInstance(this).unregisterReceiver(bStateReceiver); 

静的レジスタ:

<receiver 
    android:name=".FullyQualifiedBroadcastReceiverClassName" 
    android:enabled="true"> 
<intent-filter> 
    <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/> 
</intent-filter> 

関連する問題