[英]Connect bluetooth with getRemoteDevice() from BluetoothAdapter
I would like to manually connect a bluetooth device with its MAC address because it is faster and I know exactly which MAC to connect. 我想手动使用其MAC地址连接蓝牙设备,因为它速度更快,而且我确切知道要连接哪个MAC。
I use this method to get the BluetoothDevice : http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getRemoteDevice%28byte[]%29 我使用此方法获取BluetoothDevice: http : //developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getRemoteDevice%28byte[]%29
But the Android doc does not say if Android ensure that the device is in range before creating the BluetoothDevice object. 但是Android文档没有说明Android在创建BluetoothDevice对象之前是否确保设备在范围内。 Do you have this information ?
你有这个信息吗?
My code can automatically connect the device, and I would like to check if the target is in range before trying to connect, but without perform a large scan (which can be long...) 我的代码可以自动连接设备,并且我想在尝试连接之前检查目标是否在范围之内,但无需执行大型扫描(这可能很长...)。
When local device connects to remote device using BluetoothSocket
, an exception is required. 当本地设备使用
BluetoothSocket
连接到远程设备时,需要一个例外。
If remote device isn't in range, It's not found 如果远程设备不在范围内,则找不到它
private class ConnectThread extends Thread {
public ConnectThread(BluetoothDevice device, boolean isSecure, UUID sharedUUID) throws IncorrectSetupException {
try {
//Secure connections requires to get paired before connect
//Insecure connections allows to connect without pairing
if (isSecure) {
mSocket = device.createRfcommSocketToServiceRecord(sharedUUID);
} else {
mSocket = device.createInsecureRfcommSocketToServiceRecord(sharedUUID);
}
} catch (IOException e) {
//Is there some problem with the setup?
}
}
public void run() {
try {
mSocket.connect();
} catch (IOException e) {
//If device is not found, this exception is throwed
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.