我已经成功识别了蓝牙设备(HC-05),获得了它的名称,MAC地址和绑定状态,但是当我尝试创建客户端套接字以启动与它的蓝牙连接时,我得到了空套接字。
//initializing bluetooth adapter
val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
if (bluetoothAdapter?.isEnabled == false) {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}
//creating a list of PAIRED DEVICES
val pairedDevices: Set<BluetoothDevice>? = bluetoothAdapter?.bondedDevices
//define a BluetoothDevice (the first on the list)
pairedDevices?.forEach { device ->
val deviceName = device.name
val deviceHardwareAddress = device.address // MAC address
val arduino: BluetoothDevice = pairedDevices.first()
然后:
val arduinoSocket = arduino?.createRfcommSocketToServiceRecord(uuid)
为空。
我的UUID是:
private val uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")
有什么见解吗?
答案 0 :(得分:0)
您的代码在大多数情况下似乎都不错。 您可以尝试使用下面的代码来尝试获取设备的实际uuid,而不是使用默认的uuid。
BluetoothSocket arduinoSocket = arduino.createRfcommSocketToServiceRecord(arduino.getUuids()[0].getUuid());