简体   繁体   English

如何在Android中连接蓝牙设备

[英]How to connect the Bluetooth device in Android

I reference and imitates the Android Sample at android-sdks\\samples\\android-18\\legacy\\BluetoothLeGatt to developing an application. 我参考并模仿了android-sdks \\ samples \\ android-18 \\ legacy \\ BluetoothLeGatt上的Android示例来开发应用程序。

I can scan the device. 我可以扫描设备。 But the app crash when I try to connect the bluetooth device. 但是,当我尝试连接蓝牙设备时,应用程序崩溃。

This is the log that the app crash when I try to connect the bluetooth device. 这是我尝试连接蓝牙设备时应用程序崩溃的日志。

D/BtGatt.btif( 2443): btif_gattc_scan
D/BtGatt.btif( 2443): btgattc_handle_event: Event 1003
D/BtGatt.GattService( 2443): unregisterClient() - clientIf=4
D/BtGatt.btif( 2443): btif_gattc_unregister_app
D/BtGatt.btif( 2443): btgattc_handle_event: Event 1001
D/BtGatt.btif( 2443): btif_gattc_upstreams_evt: Event 1
I/ActivityManager( 1766): START u0 {cmp=com.example.prevent_the_lost/.DeviceControl (has extras)} from pid 5526
W/WindowManager( 1766): Screenshot failure taking screenshot for (166x125) to layer 21015
I/bt-hci  ( 2443): BLE HCI(id=62) event = 0x02)
I/bt-hci  ( 2443): btu_ble_process_adv_pkt
D/BluetoothAdapter( 5526): stopLeScan()
I/bt-hci  ( 2443): BLE HCI(id=62) event = 0x02)
I/bt-hci  ( 2443): btu_ble_process_adv_pkt
D/dalvikvm( 5526): GC_CONCURRENT freed 165K, 5% free 4427K/4624K, paused 1ms+1ms, total 5ms
W/ActivityManager( 1766): Unable to start service Intent { cmp=com.example.prevent_the_lost/.BluetoothLeService } U=0: not found
I/ActivityManager( 1766): Displayed com.example.prevent_the_lost/.DeviceControl: +29ms
D/AndroidRuntime( 5526): Shutting down VM
W/dalvikvm( 5526): threadid=1: thread exiting with uncaught exception (group=0x94ee8648)
E/AndroidRuntime( 5526): FATAL EXCEPTION: main
E/AndroidRuntime( 5526): java.lang.NullPointerException
E/AndroidRuntime( 5526):        at com.example.prevent_the_lost.DeviceControl.onOptionsItemSelected(DeviceControl.java:140)
E/AndroidRuntime( 5526):        at android.app.Activity.onMenuItemSelected(Activity.java:2566)
E/AndroidRuntime( 5526):        at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:986)
E/AndroidRuntime( 5526):        at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
E/AndroidRuntime( 5526):        at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
E/AndroidRuntime( 5526):        at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
E/AndroidRuntime( 5526):        at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
E/AndroidRuntime( 5526):        at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
E/AndroidRuntime( 5526):        at android.view.View.performClick(View.java:4240)
E/AndroidRuntime( 5526):        at android.view.View$PerformClick.run(View.java:17721)
E/AndroidRuntime( 5526):        at android.os.Handler.handleCallback(Handler.java:730)
E/AndroidRuntime( 5526):        at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 5526):        at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 5526):        at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime( 5526):        at java.lang.reflect.Method.invokeNative(NativeMethod)
E/AndroidRuntime( 5526):        at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime( 5526):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime( 5526):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime( 5526):        at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 1766):   Force finishing activity com.example.prevent_the_lost/.DeviceControl
W/WindowManager( 1766): Screenshot failure taking screenshot for (166x125) to layer 21020
W/ActivityManager( 1766): Activity pause timeout for ActivityRecord{4aad960c u0com.example.prevent_the_lost/.DeviceControl}
I/Process ( 5526): Sending signal. PID: 5526 SIG: 9
W/InputDispatcher( 1766): channel '4aaa49ec com.example.prevent_the_lost/com.example.prevent_the_lost.Device_List (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
E/InputDispatcher( 1766): channel '4aaa49ec com.example.prevent_the_lost/com.example.prevent_the_lost.Device_List (server)' ~ Channel is unrecoverably broken and will be disposed!
W/InputDispatcher( 1766): channel '4aa9c3e4 com.example.prevent_the_lost/com.example.prevent_the_lost.DeviceControl (server)' ~ Consumer closed input channel or an error occurred.  events=0xd
E/InputDispatcher( 1766): channel '4aa9c3e4 com.example.prevent_the_lost/com.example.prevent_the_lost.DeviceControl (server)' ~ Channel is unrecoverably brokenand will be disposed!
W/InputDispatcher( 1766): Attempted to unregister already unregistered input channel '4aaa49ec com.example.prevent_the_lost/com.example.prevent_the_lost.Device_List (server)'
W/InputDispatcher( 1766): Attempted to unregister already unregistered input channel '4aa9c3e4 com.example.prevent_the_lost/com.example.prevent_the_lost.DeviceControl (server)'
I/ActivityManager( 1766): Process com.example.prevent_the_lost (pid 5526) has died.
I/WindowState( 1766): WIN DEATH: Window{4aaa49ec u0 com.example.prevent_the_lost/com.example.prevent_the_lost.Device_List}
I/WindowState( 1766): WIN DEATH: Window{4aa9c3e4 u0 com.example.prevent_the_lost/com.example.prevent_the_lost.DeviceControl}
I/ActivityManager( 1766): Start proc com.example.prevent_the_lost for activity com.example.prevent_the_lost/.Device_List: pid=5545 uid=10043 gids={50043, 3001,3002, 1028}
I/ActivityThread( 5545): Switching default density from 160 to 130
D/BluetoothManagerService( 1766): Message: 20
D/BluetoothManagerService( 1766): Added callback: android.bluetooth.IBluetoothManagerCallback$Stub$Proxy@4ab1fee8:true
W/InputMethodManagerService( 1766): Got RemoteException sending setActive(false) notification to pid 5526 uid 10043
I/ActivityManager( 1766): Displayed com.example.prevent_the_lost/.Device_List: +171ms
D/dalvikvm( 1766): GC_CONCURRENT freed 455K, 19% free 7301K/8980K, paused 1ms+1ms, total 12ms 

I don't know what the error in this Log ?? 我不知道此日志中的错误是什么?

Could somebody tell me the error and how to solve it ??? 有人可以告诉我错误以及如何解决吗?

Thanks a lot!!! 非常感谢!!!

I find the error is "Unable to start service Intent" 我发现错误是“无法启动服务意图”

The solution is add the Service in Manifest -> Application. 解决方法是在清单->应用程序中添加服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM