该项目应该不断向我提供其MAC地址已知的设备的RSSI。我得到RSSI大约2-3次,然后什么也没有。即使我在Android手机上测试了该设备,我仍可以绕过Google向我抛出的电池限制等。该项目的目标设备将是植根的Android M设备。
这是我的代码:
private final BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName=device.getName();
String deviceHardwareAddress=device.getAddress();
int rssi=intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
if(deviceHardwareAddress.equals(MAC)){
deviceFound=true;
bluetoothAdapter.cancelDiscovery();
Log.i("broadcastReceiver" , "device " + deviceName);
Log.i("broadcastReceiver" , "hard" + deviceHardwareAddress);
Log.i("broadcastReceiver","Rssi "+rssi);
Log.i("broadcastReceiver","Paired Id "+MAC );
tv.setText(deviceName+" "+rssi);
stringArrayList.add(String.valueOf(rssi));
arrayAdapter.notifyDataSetChanged();
deviceFound=false;
bluetoothAdapter.startDiscovery();
}
if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
if(deviceFound==false){
bluetoothAdapter.startDiscovery();
}
}
}
}
};
答案 0 :(得分:0)
解决了!
只需将BroadcastReceiver分成两部分即可。请不要在不了解情况的情况下复制此答案。此代码将消耗大量电池。我的项目允许这样做。它将具有持续供电的功能。它仅在某些条件下运行。干杯!
private final BroadcastReceiver broadcastReceiver1=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
if(deviceFound==false) {
bluetoothAdapter.cancelDiscovery();
bluetoothAdapter.startDiscovery();
}
};
}
};
private final BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName=device.getName();
String deviceHardwareAddress=device.getAddress();
int rssi=intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
if(deviceHardwareAddress.equals(MAC)){
deviceFound=true;
bluetoothAdapter.cancelDiscovery();
Log.i("broadcastReceiver" , "device " + deviceName);
Log.i("broadcastReceiver" , "hard" + deviceHardwareAddress);
Log.i("broadcastReceiver","Rssi "+rssi);
Log.i("broadcastReceiver","Paired Id "+MAC );
tv.setText(deviceName+" "+rssi);
deviceFound=false;
bluetoothAdapter.startDiscovery();
}
}
}
};