作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从 Android Things 开发板连接到我的蓝牙扬声器。我在 DiscoveryMode 中检测到我的扬声器,但是当我尝试连接时,我的日志中出现了一些奇怪的错误。
我得到了这个广播接收器:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d("BT", "Action:" + action);
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String speaker = new String("00:02:3C:2A:02:4A");
String deviceHardwareAddress = device.getAddress(); // MAC address
Log.d("BT", "mReceiver Found device:" + deviceName + ", MAC:" + deviceHardwareAddress);
if(deviceHardwareAddress.equals(speaker)) {
Log.d("BT", "Got speaker! connecting!");
Thread thread =new ConnectThread(device);
thread.start();
}
}
}
};
我的连接线程:
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private UUID SERVICE_UUID = UUID.fromString("795090c7-420d-4048-a24e-18e60180e23c");
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket
// because mmSocket is final.
BluetoothSocket tmp = null;
mmDevice = device;
Log.d("BT", "ConnectThread device " + mmDevice.getName());
try {
// Get a BluetoothSocket to connect with the given BluetoothDevice.
// MY_UUID is the app's UUID string, also used in the server code.
tmp = device.createRfcommSocketToServiceRecord(SERVICE_UUID);
} catch (IOException e) {
Log.e("BT", "Socket's create() method failed", e);
}
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it otherwise slows down the connection.
mBluetoothAdapter.cancelDiscovery();
Log.d("BT", "ConnectThread connecting to device " + mmDevice.getName());
try {
// Connect to the remote device through the socket. This call blocks
// until it succeeds or throws an exception.
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and return.
try {
mmSocket.close();
} catch (IOException closeException) {
Log.e("BT", "Could not close the client socket", closeException);
}
return;
}
// The connection attempt succeeded. Perform work associated with
// the connection in a separate thread.
Log.e("BT", "Connected!");
//manageMyConnectedSocket(mmSocket);
}
// Closes the client socket and causes the thread to finish.
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "Could not close the client socket", e);
}
}
}
还有我的日志:
01-01 00:02:10.014 393-495/? W/bt_btm_ble: btm_ble_process_adv_pkt_cont device no longer discoverable, discarding advertising packet
01-01 00:02:10.210 393-475/? D/bt_btif_config: btif_get_device_type: Device [00:02:3c:2a:02:4a] type 1
01-01 00:02:10.217 1443-1443/kot.ninja.iot D/BT: Action:android.bluetooth.device.action.FOUND
01-01 00:02:10.220 1443-1443/kot.ninja.iot D/BT: mReceiver Found device:Creative D200, MAC:00:02:3C:2A:02:4A
01-01 00:02:10.220 1443-1443/kot.ninja.iot D/BT: Got speaker! connecting!
01-01 00:02:10.228 1443-1443/kot.ninja.iot D/BT: ConnectThread device Creative D200
01-01 00:02:10.253 1443-1476/kot.ninja.iot D/BT: ConnectThread connecting to device Creative D200
01-01 00:02:11.579 393-495/? W/bt_btif: bta_dm_acl_change info: 0x10
01-01 00:02:11.580 393-475/? D/bt_btif_dm: remote version info [00:02:3c:2a:02:4a]: 0, 0, 0
01-01 00:02:11.803 393-495/? W/bt_sdp: process_service_search_attr_rsp
01-01 00:02:11.803 393-495/? W/bt_sdp: sdp_copy_raw_data: list_len:0 cpy_len:1800 p:0x8f9b780a p_ccb:0x91f4f2b0 p_db:0x91ed8624 raw_size:1800 raw_used:0 raw_data:0x91ed7f1c
01-01 00:02:11.814 393-519/? E/bt_btif_sock_rfcomm: find_rfc_slot_by_id unable to find RFCOMM slot id: 4
01-01 00:02:15.909 393-495/? I/bt_btm_sec: btm_sec_disconnected clearing pending flag handle:12 reason:22
这个错误是什么意思?
01-01 00:02:11.814 393-519/? E/bt_btif_sock_rfcomm: find_rfc_slot_by_id unable to find RFCOMM slot id: 4
也许我需要将这两个设备配对?我正在尝试关注 https://developer.android.com/guide/topics/connectivity/bluetooth-le.html
最佳答案
这是因为安卓手机屏蔽了BLE包。事实上,ESP32 设备正在广播。您可以使用 BLE 嗅探器来验证这一点。我想,也许android手机接受的广播包必须满足一定的规则。你可以试试这个adv数据
关于Android Things 如何连接蓝牙音箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46138278/
我是一名优秀的程序员,十分优秀!