问题遇到的现象和发生背景
我正在做 Android 蓝牙spp的项目
(用app 连接一个spp蓝牙设备)
现在在连接的部分有问题 系统默认显示配对后 不继续进行连接 不知道我写的有啥问题
用代码块功能插入代码,请勿粘贴截图
//这是uuid
private static final UUID sppuuid = UUID.fromString(
"00001101-0000-1000-8000-00805F9B34FB");
//这是我添加的点击事件 里面调用了一个连接线程
listView.setOnItemClickListener((adapterView, view, i, l) -> {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
//从蓝牙信息中截取Address
String[] string = listView.getItemAtPosition(i).toString().split(":");
BluetoothDevice device = adapter.getRemoteDevice(string[2].trim());
ConnectThread connectThread = new ConnectThread(device);
connectThread.start();
});
//这是线程
class ConnectThread extends Thread {
private BluetoothSocket mmSocket;
private BluetoothDevice mmDevice;
@SuppressLint("MissingPermission")
public ConnectThread(BluetoothDevice device) {
this.mmDevice = device;
BluetoothSocket tmp = null;
try {
tmp = device.createRfcommSocketToServiceRecord(sppuuid);
} catch (IOException e) {
e.printStackTrace();
}
mmSocket = tmp;
}
@SuppressLint("MissingPermission")
public void run() {
// try {
// try (BluetoothSocket socket = (BluetoothSocket) mmDevice.getClass().getDeclaredMethod(
// "createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, 1)) {
//先退出扫描再进行连接
blueadApter.cancelDiscovery();
//发起配对连接
// assert socket != null;
try {
mmSocket.connect();
} catch (IOException ex) {
try {
mmSocket.close();
} catch (IOException e2) {
e2.printStackTrace();
}
// } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | IOException e) {
// e.printStackTrace();
}
}
}
运行结果及报错内容
在扫描出蓝牙设备后 我点击蓝牙设备名想去进行连接 这时会弹出来系统弹框 问要不要配对 我点击确定之后就没有后文了 也不会继续进行连接 很奇怪
我的解答思路和尝试过的方法
我试过 new一个Thread去跑connect 但还是没有用 不知道是哪里出问题
我想要达到的结果
希望有人能帮忙解决