アクティビティを開始するとき、Bluetooth ペアリングされたデバイスがあるかどうかを知り、その結果に応じて何かを行う必要があります。このために、私は次のことを行います。
onResume で:
protected void onResume() {
super.onResume();
/**Filters para comprobar el BroadcastReceiver*/
IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(mReceiver, filter1);
this.registerReceiver(mReceiver, filter2);
次に、BroadcastReceiver を使用します。
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
/**Do something if connected*/
if(action.equals("android.bluetooth.device.action.ACL_CONNECTED")) {
//Action
}
/**Do something if disconnected*/
else if (action.equals("android.bluetooth.device.action.ACL_DISCONNECTED")) {
Toast.makeText(getApplicationContext(), "No device paired", Toast.LENGTH_SHORT).show();
}
}
};
マニフェストで:
<activity
android:name=".Configuration"
android:label="@string/config_title" >
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED"/>
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED"/>
</intent-filter>
</activity>
しかし、Still はやるべきだと思っていることをしていないので、何かが間違っているか、何かをするのを忘れていました。