java 蓝牙指定连接失败_Android蓝牙socket.connect失败;引发Java IO异常

我正在使用从Android开发人员页面上的蓝牙教程衍生的代码.

我的应用程序尝试连接到BT秤并记录重量信息.

我的程序在尝试执行BluetoothSocket.connect()时抛出错误.

该程序作为蓝牙客户端/从机运行,以便从BT设备接收数据.

用户在屏幕上看到已配对设备的列表,然后单击特定设备以连接到该设备.

因此,当用户单击特定设备时,由于该设备已预先配对,因此应用程序尝试连接到该设备.

该应用程序获取了BluetoothSocket,但无法执行connect()

我试图与其他蓝牙设备建立连接,但最终总是反复出现相同的错误.

以下是connectThread的Java代码:

private class ConnectThread extends Thread {

private final BluetoothSocket mmSocket;

private final BluetoothDevice mmDevice;

public ConnectThread(BluetoothDevice device) {

// Use a temporary object that is later assigned to mmSocket,

// because mmSocket is final

BluetoothSocket tmp = null;

mmDevice = device;

Log.i(tag, "construct");

// Get a BluetoothSocket to connect with the given BluetoothDevice

try {

// MY_UUID is the app's UUID string, also used by the server code

tmp = device.createRfcommSocketToServiceRecord(MY_UUID);

} catch (IOException e) {

Log.i(tag, "get socket failed");

}

mmSocket = tmp;

}

public void run() {

// Cancel discovery because it will slow down the connection

btAdapter.cancelDiscovery();

Log.i(tag, "connect - run");

try {

// Connect the device through the socket. This will block

// until it succeeds or throws an exception

mmSocket.connect();

Log.i(tag, "connect - succeeded");

} catch (IOException connectException) { Log.i(tag, "connect failed");

// Unable to connect; close the socket and get out

try {

mmSocket.close();

} catch (IOException closeException) { }

return;

}

// Do work to manage the connection (in a separate thread)

mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();

}

该应用程序的完整代码可以找到here

以下是我在运行该应用程序时获得的日志:

01-01 01:46:00.359: I/ActivityManager(1553): Displayed com.test.bluetooth/.Main_Activity: +579ms

01-01 01:46:00.953: D/BluetoothService(1553): updateDeviceServiceChannelCache(00:10:C6:2E:CB:C3)

01-01 01:46:00.968: D/BluetoothService(1553): updateDeviceServiceChannelCache(00:19:15:66:C4:2D)

01-01 01:46:00.984: D/BluetoothService(1553): updateDeviceServiceChannelCache(00:1A:88:00:01:25)

01-01 01:46:01.000: D/BluetoothService(1553): uuid(system): 00001105-0000-1000-8000-00805f9b34fb 1

01-01 01:46:04.421: I/debugging(2400): construct

01-01 01:46:04.421: D/BluetoothEventLoop(1553): Property Changed: Discovering : false

01-01 01:46:04.421: I/debugging(2400): in click listener

01-01 01:46:04.421: E/BluetoothService.cpp(1553): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)

01-01 01:46:04.429: E/BluetoothService.cpp(1553): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)

01-01 01:46:04.437: I/debugging(2400): connect - run

01-01 01:46:04.437: V/BluetoothDiscoveryReceiver(1864): Received: android.bluetooth.adapter.action.DISCOVERY_FINISHED

01-01 01:46:04.445: E/BluetoothEventLoop.cpp(1553): onCreateDeviceResult: D-Bus error: org.bluez.Error.AlreadyExists (Already Exists)

01-01 01:46:05.484: D/BluetoothEventLoop(1553): Device property changed: 00:10:C6:2E:CB:C3 property: Connected value: true

01-01 01:46:06.554: D/BluetoothService(1553): updateDeviceServiceChannelCache(00:10:C6:2E:CB:C3)

01-01 01:46:06.562: D/BluetoothService(1553): uuid(application): 00001101-0000-1000-8000-00805f9b34fb 1

01-01 01:46:06.562: D/BluetoothService(1553): Making callback for 00001101-0000-1000-8000-00805f9b34fb with result 1

01-01 01:46:06.562: I/debugging(2400): connect failed

01-01 01:46:06.562: W/System.err(2400): java.io.IOException: Invalid argument

01-01 01:46:06.562: W/System.err(2400): at android.bluetooth.BluetoothSocket.connectNative(Native Method)

01-01 01:46:06.562: W/System.err(2400): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:216)

01-01 01:46:06.562: W/System.err(2400): at com.test.bluetooth.Main_Activity$ConnectThread.run(Main_Activity.java:326)

01-01 01:46:10.132: D/BluetoothEventLoop(1553): Device property changed: 00:10:C6:2E:CB:C3 property: Connected value: false

01-01 01:47:05.914: D/SurfaceFlinger(1311): About to give-up screen, flinger = 0x41876af0

?非常感谢为建立基本BT连接提供的任何/所有帮助.

谢谢.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值