android things 蓝牙,Android Things how to connect to bluetooth speaker

Im trying to connect to my bluetooth speaker from Android Things board. Im detecting my speaker in DiscoveryMode, but when I try to connect I have some strange error in my logs.

I got this broadcastreciver:

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();

}

}

}

};

My 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);

}

}

}

And my logs:

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

What this error mean?

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值