簡體   English   中英

Android Things如何連接到藍牙揚聲器

[英]Android Things how to connect to bluetooth speaker

我正在嘗試從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

這是因為android手機會阻止BLE軟件包。 實際上,ESP32設備正在廣播。 您可以使用BLE嗅探器進行驗證。 我認為,也許Android手機接受的廣播程序包必須符合某些規則。 您可以嘗試此廣告數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM