问题描述
- Android 蓝牙ble连接前一定需要配对么?
-
Android 蓝牙ble连接前一定需要配对么?从网上查资料有人专做配对的,但是我要求连接是不需要配对的。我按照官方文档上写的代码,发现写出来连接前需要配对,有没有做过的人帮忙解答一下。
解决方案
BLE是不需要配对的,可能你找到的代码不对吧
解决方案二:
public boolean connect(final String address) {
if (mBluetoothAdapter == null || address == null) {
L.e("BluetoothAdapter not initialized or unspecified address.");
return false;
}
//如果以前连接过
if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
&& mBluetoothGatt != null) {
L.e("Trying to use an existing mBluetoothGatt for connection.");
if (mBluetoothGatt.connect()) {
mConnectionState = STATE_CONNECTING;
return true;
} else {
return false;
}
}
//没有连接过
bluetoothDevice = mBluetoothAdapter.getRemoteDevice(address);
if (bluetoothDevice == null) {
L.e("Device not found. Unable to connect.");
return false;
}else{
//false代表自动连接
mBluetoothGatt = bluetoothDevice.connectGatt(this, false, mGattCallback);
L.e("Trying to create a new connection.");
mBluetoothDeviceAddress = address;
mConnectionState = STATE_CONNECTING;
return true;
}
}
解决方案三:
请问为什么我的手机蓝牙检测不到从机呢?
时间: 2024-10-26 08:31:11