[英]How to programmatically connect to a Bluetooth device after it's already paired (bonded) in Android
I have been working on trying to get a Bluetooth device like a keyboard or a remote to connect to an android device.我一直在努力尝试让蓝牙设备(如键盘或遥控器)连接到 android 设备。 More specifically when this program runs for the first time it would scan for Bluetooth devices and attempt to pair and connect with one that it finds.
更具体地说,当这个程序第一次运行时,它会扫描蓝牙设备并尝试与它找到的设备配对和连接。 I have tried seemingly every possible way to accomplish this but I am only able to pair the device, not connect it completely.
我已经尝试了似乎所有可能的方法来实现这一点,但我只能配对设备,不能完全连接它。 I have tried the examples in the Android Bluetooth guide and many others.
我已经尝试过 Android 蓝牙指南和许多其他指南中的示例。 One consistency is the javi.io error I get when the BluetoothSocket is calling connect.
一种一致性是当 BluetoothSocket 调用连接时出现 javi.io 错误。
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:505)
at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:482)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:324)
at BTConnectThread.run(BTConnectThread.java:61)
I have tried different UUIDs.我尝试了不同的 UUID。 Some I generated myself others I pulled from the devices.
有些是我自己生成的,有些是我从设备中提取的。 I also tried writing code assuming both are acting as servers that mirrors mostly what I am doing here and what is in the Android Bluetooth guide.
我还尝试编写代码,假设两者都充当服务器,主要反映我在这里所做的事情以及 Android 蓝牙指南中的内容。 have tried all variations of calling createBond() on the device.
已尝试在设备上调用 createBond() 的所有变体。 All attempts leave the device paired/bonded but not connected.
所有尝试都会使设备配对/绑定但未连接。 Any help is greatly appreciated.
任何帮助是极大的赞赏。 ` public BTConnectThread(BluetoothDevice bluetoothDevice) {
`公共BTConnectThread(蓝牙设备蓝牙设备){
BluetoothSocket tempSocket = null;
try {
// tempSocket = bluetoothDevice.createRfcommSocketToServiceRecord(WELL_KNOWN_UUID);
// tempSocket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(WELL_KNOWN_UUID);
//Magic?
Method method = bluetoothDevice.getClass().getMethod("createRfcommSocket",
new Class[]{int.class});
tempSocket = (BluetoothSocket) method.invoke(bluetoothDevice, 1);
} catch (Exception e) {
e.printStackTrace();
}
m_bluetoothSocket = tempSocket;
}
public void run() {
//cancel discovery
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null)
bluetoothAdapter.cancelDiscovery();
//TODO: Try brute force approach. Loop until it connects.
//TODO: Try a fallback socket.
try {
m_bluetoothSocket.connect();
Log.d(TAG, "Connection Established");
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
Log.d(TAG, "Fail to connect!", connectException);
try {
m_bluetoothSocket.close();
} catch (IOException closeException) {
Log.d(TAG, "Fail to close connection", closeException);
}
return;
}
}
public void cancel() {
try {
m_bluetoothSocket.close();
} catch (IOException e) {
}
}`
蓝牙连接需要创建 3 个以上的线程,因此您可以尝试使用https://android-arsenal.com/details/1/1859 。
Connect Function
连接功能
fun connect(btDevice: BluetoothDevice?){
val id: UUID = btDevice?.uuids?.get(0)!!.uuid
val bts = btDevice.createRfcommSocketToServiceRecord(id)
bts?.connect()
}
Call this in main thread
在主线程中调用它
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
val device = bluetoothAdapter.getRemoteDevice("your mac address")
connect(device)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.