im试图自动连接以共享首选项保存的特定蓝牙设备。在我的情况下,如果蓝牙连接丢失,那么我将尝试每5秒连接一次相同的设备,如果发现它,那么我想自动连接
// Create a BroadcastReceiver for bluetooth related checks
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//We don't want to reconnect to already connected device
// 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);
// Check if the found device is one we had comm with
if(!getSharedPreferencesData("address","").equals(""))
{
String address = getSharedPreferencesData("address","");
if(device.getAddress().equals(address)==true)
chatController.connect(address);
//sendMessage("00000000");
}
}
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
Log.e("BT:","Connected");
sendMessage("*00000000000000#");
}else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
bluetoothAdapter.startDiscovery();
}
}, 5000);
}
}
};
我已经尝试过这段代码,但是对我来说却不起作用