Android Things如何连接到蓝牙音箱

时间:2017-09-10 07:22:38

标签: android bluetooth bluetooth-lowenergy android-things

我试图从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();
             }
         }
     }
 };

我的ConnectThread:

 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

1 个答案:

答案 0 :(得分:0)

这是因为android手机会阻止BLE程序包。实际上,ESP32设备正在广播。您可以使用BLE嗅探器进行验证。 我认为,也许Android手机接受的广播程序包必须符合某些规则。 您可以尝试此广告数据