每当蓝牙设备断开连接时,我都会使用广播接收器。我将mac设备设置为蓝牙设备,它应该通过设置做出反应,但无论我做什么,它都会在每个蓝牙设备上被触发。这是我的接收者:
public class Bluetooth_Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
SharedPreferences bluetooth = context.getSharedPreferences("BluetoothDevice", Context.MODE_PRIVATE);
String deviceMac = bluetooth.getString("MAC", "22:22:97:1F:D6:0F");
BluetoothDevice device = intent.getParcelableExtra(deviceMac);
Toast.makeText(context, "" + String.valueOf(device), Toast.LENGTH_SHORT).show();
if (deviec.ACTION_FOUND.equals(action)) {
//Device found
}
else if (device.ACTION_ACL_CONNECTED.equals(action)) {
//Device is now connected
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//Done searching
}
else if (device.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
//Device is about to disconnect
}
else if (device.ACTION_ACL_DISCONNECTED.equals(action)) {
//Device has disconnected
context.startService(new Intent(context, save_location_service.class));
}
}
}
我希望我的接收器只对断开时的特定设备作出反应(但我仍需要其他方法,以备将来使用)。 吐司给了我" null"。 deviceMac字符串给我一个MAC正确的地址。我想我可能需要创建新的蓝牙设备,但我找不到办法做到这一点...感谢任何帮助。
答案 0 :(得分:0)
if(Objects.equals(String.valueOf(device), deviceMac)){
else if (device.ACTION_ACL_DISCONNECTED.equals(action)) {
context.startService(new Intent(context, save_location_service.class));
}
}