Android无法检测蓝牙设备
我的广播接收器有问题,正在查找特定设备。在我的程序的前面,我查找当前设备,并允许用户选择。该名称和地址已保存。当应用程序处理其“onResume”时,我创建了一个广播接收器 我所要做的就是查看以前保存的设备是否在该区域中。它不需要配对,但至少必须被看到。我没有看到这些祝酒词。在此之前,我甚至尝试将设备置于可发现模式 这是我的广播接收器Android无法检测蓝牙设备,android,broadcastreceiver,android-bluetooth,Android,Broadcastreceiver,Android Bluetooth,我的广播接收器有问题,正在查找特定设备。在我的程序的前面,我查找当前设备,并允许用户选择。该名称和地址已保存。当应用程序处理其“onResume”时,我创建了一个广播接收器 我所要做的就是查看以前保存的设备是否在该区域中。它不需要配对,但至少必须被看到。我没有看到这些祝酒词。在此之前,我甚至尝试将设备置于可发现模式 这是我的广播接收器 mBluetoothReceiver = new BroadcastReceiver() { @Override public void onRe
mBluetoothReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
try {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceAddress = device.getAddress();
Toast toast = Toast.makeText(getApplicationContext(), "BT Found", Toast.LENGTH_LONG);
toast.show();
if (deviceAddress.equals(mDeviceAddress)) {
Toast toasty = Toast.makeText(getApplicationContext(), "Correct BT Device Found", Toast.LENGTH_LONG);
toasty.show();
}
}
if(BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceAddress = device.getAddress();
Toast toast = Toast.makeText(getApplicationContext(), "BT Connected", Toast.LENGTH_LONG);
toast.show();
if (deviceAddress.equals(mDeviceAddress)) {
Toast toasty = Toast.makeText(getApplicationContext(), "Correct BT Device Connected", Toast.LENGTH_LONG);
toasty.show();
}
}
} catch (Exception e) {
System.out.println("Broadcast Error : " + e.toString());
}
}
};
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mBluetoothReceiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mBluetoothReceiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
this.registerReceiver(mBluetoothReceiver, filter);
filter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
this.registerReceiver(mBluetoothReceiver, filter);
filter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
this.registerReceiver(mBluetoothReceiver, filter);
filter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(mBluetoothReceiver, filter);
编辑
似乎已正确检测到连接和断开事件。它们仅在更改时检测到。但是,找不到的操作。我不能只是在设备区域。您设置权限了吗?是的,权限已经设置好了。所以它看起来有些有效。我必须去另一个活动,然后再回到这个活动。此寄存器位于onResume代码中。为什么第一次不起作用,但其他所有的原因都起作用了?