我很难让我的Android应用程序连接到亚马逊基础知识蓝牙键盘。我可以手动将键盘与Android设备配对,没问题。但是,以编程方式执行此操作失败。该应用程序成功将设备识别为Amazon Basic键盘并获取MAC地址,它将无法连接。我试过在2个不同的Android设备上运行代码(一个运行2.3.3;另一个运行4.0.3),但结果是一样的。
尝试记录的方法(代码片段):
private final UUID MY_UUID = UUID.fromString("b1b9aee2-a388-11e4-89d3-123b93f75cba"); // I generated this UUID
...
tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
tmp.connect();
这失败了:
java.io.IOException:服务发现失败
我也尝试使用各种“标准”UUID,例如00001101-0000-1000-8000-00805F9B34FB,00001124-0000-1000-8000-00805F9B34FB,但错误相同。我在尝试连接之前也取消了Device Discovery。
我也尝试了未记录的方法(代码片段):
BluetoothSocket socket;
Method m;
m = device.getClass().getMethod("createRfcommSocket",new Class[] { int.class });
socket = (BluetoothSocket) m.invoke(device, 1);
mBluetoothAdapter.cancelDiscovery();
socket.connect();
这也失败了,但有一个不同的错误:
java.io.IOException:拒绝连接
我已经尝试了这两种方法,但设备已配对且未配对且结果相同。在连接之前我也使用了一个不太可能的Thread.sleep(5000)而没有成功。经过几天的尝试,我不知道该怎么做。
非常感谢任何帮助!
由于 科林
更新28/02/2015 :这显然是一个难以解决的问题。为了尝试和帮助我发布了我的一个运行的完整日志输出,看看这是否有帮助。
02-28 11:06:22.721: I/ActivityStack(201): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 pkg=com.example.bluetoothtest cmp=com.example.bluetoothtest/.MainActivity} from pid 547
02-28 11:06:22.721: V/ActivityStack(201): Prepare open transition: starting ActivityRecord{41a90e60 com.example.bluetoothtest/.MainActivity}
02-28 11:06:22.768: V/ActivityStack(201): Prepare open transition: prev=ActivityRecord{41a972d0 com.amazon.kindle.otter/.Launcher}
02-28 11:06:22.775: D/bluetooth2(996): ...Bluetooth ON...
02-28 11:06:22.775: D/bluetooth2(996): ...onResume - try connect...
02-28 11:06:22.775: D/bluetooth2(996): Socket Created!
02-28 11:06:22.775: D/BluetoothService.cpp(201): stopDiscoveryNative
02-28 11:06:22.775: E/BluetoothService.cpp(201): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
02-28 11:06:22.775: D/bluetooth2(996): ...Connecting...
02-28 11:06:22.775: D/BluetoothService.cpp(201): createDeviceNative
02-28 11:06:22.775: D/BluetoothService.cpp(201): ... address = 90:7F:61:97:AE:BE
02-28 11:06:22.783: D/BluetoothEventLoop.cpp(201): onCreateDeviceResult
02-28 11:06:22.783: D/BluetoothEventLoop.cpp(201): ... Address = 90:7F:61:97:AE:BE
02-28 11:06:22.783: E/BluetoothEventLoop.cpp(201): onCreateDeviceResult: D-Bus error: org.bluez.Error.AlreadyExists (Already Exists)
02-28 11:06:22.783: D/BluetoothEventLoop(201): Result of onCreateDeviceResult:1
02-28 11:06:22.783: D/BluetoothService.cpp(201): discoverServicesNative
02-28 11:06:22.783: D/BluetoothService.cpp(201): ... Object Path = /org/bluez/121/hci0/dev_90_7F_61_97_AE_BE
02-28 11:06:22.783: D/BluetoothService.cpp(201): ... Pattern = , strlen = 0
02-28 11:06:22.783: I//system/bin/bluetoothd(110): bluetoothd[121]: external/bluetooth/bluez/src/device.c:btd_device_ref() 0x1e90a88: ref=3
02-28 11:06:22.924: D/dalvikvm(201): GC_EXPLICIT freed 415K, 21% free 21196K/26503K, paused 3ms+9ms
02-28 11:06:23.455: I//system/bin/bluetoothd(110): bluetoothd[121]: external/bluetooth/bluez/plugins/hciops.c:conn_complete() status 0x00
02-28 11:06:23.471: D/BluetoothEventLoop.cpp(201): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/121/hci0/dev_90_7F_61_97_AE_BE
02-28 11:06:23.471: D/BluetoothEventLoop(201): Device property changed: 90:7F:61:97:AE:BE property: Connected value: true
02-28 11:06:23.479: I//system/bin/bluetoothd(110): bluetoothd[121]: external/bluetooth/bluez/plugins/hciops.c:remote_features_information() hci0 status 0
02-28 11:06:23.479: I//system/bin/bluetoothd(110): bluetoothd[121]: external/bluetooth/bluez/plugins/hciops.c:remote_name_information() hci0 status 0
02-28 11:06:23.682: I//system/bin/bluetoothd(110): bluetoothd[121]: external/bluetooth/bluez/src/device.c:search_cb() 90:7F:61:97:AE:BE: No service update
02-28 11:06:23.682: D/BluetoothEventLoop.cpp(201): onDiscoverServicesResult
02-28 11:06:23.682: D/BluetoothEventLoop.cpp(201): ... Device Path = /org/bluez/121/hci0/dev_90_7F_61_97_AE_BE
02-28 11:06:23.682: D/BluetoothService.cpp(201): getDevicePropertiesNative
02-28 11:06:23.682: I//system/bin/bluetoothd(110): bluetoothd[121]: external/bluetooth/bluez/src/device.c:btd_device_unref() 0x1e90a88: ref=2
02-28 11:06:23.689: D/BluetoothService(201): updateDeviceServiceChannelCache(90:7F:61:97:AE:BE)
02-28 11:06:23.689: D/BluetoothService(201): Cleaning up failed UUID channel lookup: 90:7F:61:97:AE:BE 00001101-0000-1000-8000-00805f9b34fb
02-28 11:06:23.689: D/bluetooth2(996): ....Connection failed...
02-28 11:06:23.689: W/System.err(996): java.io.IOException: Service discovery failed
02-28 11:06:23.689: W/System.err(996): at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:397)
02-28 11:06:23.689: W/System.err(996): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:207)
02-28 11:06:23.689: W/System.err(996): at com.example.bluetoothtest.MainActivity.onResume(MainActivity.java:138)
02-28 11:06:23.689: W/System.err(996): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1154)
02-28 11:06:23.689: W/System.err(996): at android.app.Activity.performResume(Activity.java:4711)
02-28 11:06:23.689: W/System.err(996): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2516)
02-28 11:06:23.689: W/System.err(996): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2562)
02-28 11:06:23.689: W/System.err(996): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2065)
02-28 11:06:23.689: W/System.err(996): at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-28 11:06:23.689: W/System.err(996): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1181)
02-28 11:06:23.689: W/System.err(996): at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 11:06:23.689: W/System.err(996): at android.os.Looper.loop(Looper.java:137)
02-28 11:06:23.689: W/System.err(996): at android.app.ActivityThread.main(ActivityThread.java:4558)
02-28 11:06:23.689: W/System.err(996): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 11:06:23.689: W/System.err(996): at java.lang.reflect.Method.invoke(Method.java:511)
02-28 11:06:23.689: W/System.err(996): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-28 11:06:23.689: W/System.err(996): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-28 11:06:23.689: W/System.err(996): at dalvik.system.NativeStart.main(Native Method)
02-28 11:06:23.791: I/ActivityManager(201): Displayed com.example.bluetoothtest/.MainActivity: +1s23ms
02-28 11:06:24.814: V/Sensors(201): accel data: 0.028740 0.076641 9.091495
02-28 11:06:28.088: I//system/bin/bluetoothd(110): bluetoothd[121]: external/bluetooth/bluez/plugins/hciops.c:disconn_complete() handle 12 status 0x00
02-28 11:06:28.088: I//system/bin/bluetoothd(110): bluetoothd[121]: external/bluetooth/bluez/src/event.c:btd_event_disconn_complete()
02-28 11:06:28.088: I//system/bin/bluetoothd(110): bluetoothd[121]: external/bluetooth/bluez/src/adapter.c:adapter_remove_connection()
02-28 11:06:28.088: D/BluetoothEventLoop.cpp(201): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/121/hci0/dev_90_7F_61_97_AE_BE
02-28 11:06:28.088: D/BluetoothEventLoop(201): Device property changed: 90:7F:61:97:AE:BE property: Connected value: false