BluetoothDevice 对自定义 MAC 地址做出反应
BluetoothDevice react on custom MAC address
我有一个广播接收器,每当蓝牙设备断开连接时都会使用它。我为蓝牙设备设置了 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 正确的地址。我想我可能需要创建新的蓝牙设备,但我无法找到执行此操作的方法...感谢任何帮助。
好吧,哇,经过几天的努力寻找解决方案后,我找到了一个简单的方法,但它确实有效。我添加 if 语句,检查 MAC 是否相同,我已保存在共享首选项中。
if(Objects.equals(String.valueOf(device), deviceMac)){
else if (device.ACTION_ACL_DISCONNECTED.equals(action)) {
context.startService(new Intent(context, save_location_service.class));
}
}
我有一个广播接收器,每当蓝牙设备断开连接时都会使用它。我为蓝牙设备设置了 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 正确的地址。我想我可能需要创建新的蓝牙设备,但我无法找到执行此操作的方法...感谢任何帮助。
好吧,哇,经过几天的努力寻找解决方案后,我找到了一个简单的方法,但它确实有效。我添加 if 语句,检查 MAC 是否相同,我已保存在共享首选项中。
if(Objects.equals(String.valueOf(device), deviceMac)){
else if (device.ACTION_ACL_DISCONNECTED.equals(action)) {
context.startService(new Intent(context, save_location_service.class));
}
}