jul*_*eam 7 java sockets android bluetooth ioexception
我正在尝试创建一个基本的蓝牙应用程序,用于测试设备.
我从developer.android获得了代码.这是链接:http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingDevices
这是我的线程代码的一部分:
public void run() {
mBluetoothAdapter.cancelDiscovery();
Log.i(TAG, "Discovery Cancel!");
try {
Log.i(TAG, "Connection Started");
mmSocket.connect();
Log.i(TAG, "Connection Ended");
} catch (IOException e) {
try {
Log.e(TAG, "Connection Failed", e);
mmSocket.close();
} catch (IOException e2) {
Log.e(TAG, "Connection Close Failed", e2);
}
return;
}
Run Code Online (Sandbox Code Playgroud)
无论我尝试mmSocket.connect();过什么都行不通.总是抛出IOException并从logcat中获取该日志:
java.io.IOException: Service discovery failed
at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:403)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:213)
我看了这些文章,并尝试了写的东西,没有一个解决了我的问题.
Android蓝牙java.io.IOException:连接被拒绝了?
顺便说一句,我正在研究android ics 4.0.4.
我知道这不是设备问题,因为我在不同的设备上尝试过这个应用程序.
它为我工作
BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(device, 1);
bluetoothAdapter.cancelDiscovery();
socket.connect();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15143 次 |
| 最近记录: |