简体   繁体   English

未连接设备时Android蓝牙崩溃

[英]Android Bluetooth crash when device is not connected

I'm having a serial Bluetooth device connected to my android app. 我正在将串行蓝牙设备连接到我的android应用程序。 It works well, but only when the device is on.. 它工作正常,但仅当设备打开时才有效。

When my bluetooth device ("Gps_3.3v_4800") device is off my app crashes when it starts. 当我的蓝牙设备(“ Gps_3.3v_4800”)设备关闭时,我的应用程序启动时会崩溃。 Is there a way to see if its on? 有没有办法查看它是否开启? Or anyway to prevent my app from crashing. 或者无论如何防止我的应用崩溃。

for (BluetoothDevice device : pairedDevices) {
                btDevice[i] = device.getName();
                System.out.println(device.getName() + device.getAddress() + " \n");

                // Connect to my device
                if(device.getName().equalsIgnoreCase("Gps_3.3v_4800")) {
                        Thread ct = new ConnectThread(device, mBluetoothAdapter);
                        isct = new ConnectedThread(((ConnectThread) ct).getSocket());
                        ((ConnectedThread)isct).setJavascript(javascr);
                        ((ConnectedThread)isct).write("hallo");
                        isct.start();
                }
}

My error when device is not on: 设备未开机时出现我的错误:

Fatal signal 11 (SIGSEGV) at 0x00000008 (code=1), thread 3928 (Thread-4174) 致命信号11(SIGSEGV)位于0x00000008(代码= 1),线程3928(线程4174)

Update Now I'm getting a null pointer (java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hellobt/com.example.hellobt.MainActivity}: java.lang.NullPointerException) Trying to stop the thread when it wont connect.. 立即更新我得到一个空指针(java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.hellobt / com.example.hellobt.MainActivity}:java.lang.NullPointerException)尝试在线程停止时停止不会连接..

Solved the problem with checking: 通过检查解决了问题:

if(mmSocket.isConnected()) 如果(mmSocket.isConnected())

I think there is only a problem when a phone is connected with another device. 我认为只有手机与其他设备连接时才有问题。 But for me that is ok. 但是对我来说没关系。

Thank you all. 谢谢你们。

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

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