【问题标题】:app crashes when no bluetooth receiver is turned off未关闭蓝牙接收器时应用程序崩溃
【发布时间】:2014-03-23 01:31:17
【问题描述】:

我已经设置了蓝牙连接,除非目标接收器关闭,否则一切正常。 findBT() 设置为检测配对设备名称,但即使设备关闭,也始终设置 isBTConnected=true。 pairedDevices.size() 也应该为 0,因为我在范围内没有任何其他设备,这也应该设置 isBTConnected = false。我不知道从哪里着手检测没有可用的蓝牙。

        void findBT() {
    mmDevice = null;
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        msbox("BlueTooth ", "This device does not support bluetooth");
        isBTConnected = false;

    } else {

        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBluetooth, 0);
        }

    }

    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    if (pairedDevices.size() > 0) {

        for (BluetoothDevice device : pairedDevices) {
            // if(device.getName().equals("RN42-45A9"))
            if (device.getName().equals("RN42-45A9")) {
                Log.d("TAG", "BT true");
                mmDevice = device;
                isBTConnected = true;


            } else {

                isBTConnected = false;
                Log.d("TAG", "BT false");
                msbox("BlueTooth ", "You must pair device");
            }

        }
    } else {
        isBTConnected = false;
        msbox("BlueTooth ",
                "You must pair this device with the Mobil-Link receiver before you can run application. Go to settings> Bluetooth> Pairing or see intruction manual for "
                        + "for pairing device");
    }
}

void openBT() throws IOException {
    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // Standard

    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
    mmSocket.connect();
    mmOutputStream = mmSocket.getOutputStream();
    mmInputStream = mmSocket.getInputStream();

    mConnectedThread = new ConnectedThread(mmSocket);
    mConnectedThread.start();

}

我改变了 openBT()

        void openBT() throws IOException {

    if (mmDevice==null){
        isBTConnected = false;
        Log.d("TAG", "BT NULL");
        return;
    }


    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // Standard

    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
    mmSocket.connect();
    mmOutputStream = mmSocket.getOutputStream();
    mmInputStream = mmSocket.getInputStream();

    mConnectedThread = new ConnectedThread(mmSocket);
    mConnectedThread.start();

}

Logcat

03-22 22:02:35.563: D/TAG(11971): CON  true
03-22 22:02:35.563: W/BluetoothAdapter(11971): getBluetoothService() called with no BluetoothManagerCallback
03-22 22:02:35.563: D/BluetoothSocket(11971): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[41]}
03-22 22:02:40.719: W/System.err(11971): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
03-22 22:02:40.719: W/System.err(11971):    at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:504)
03-22 22:02:40.719: W/System.err(11971):    at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:515)
03-22 22:02:40.719: W/System.err(11971):    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:319)
03-22 22:02:40.729: W/System.err(11971):    at com.example.simplergb.SimpleRGB_Main.openBT(SimpleRGB_Main.java:3075)
03-22 22:02:40.729: W/System.err(11971):    at com.example.simplergb.SimpleRGB_Main.onCreate(SimpleRGB_Main.java:262)
03-22 22:02:40.729: W/System.err(11971):    at android.app.Activity.performCreate(Activity.java:5133)
03-22 22:02:40.729: W/System.err(11971):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-22 22:02:40.729: W/System.err(11971):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-22 22:02:40.729: W/System.err(11971):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-22 22:02:40.729: W/System.err(11971):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-22 22:02:40.729: W/System.err(11971):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-22 22:02:40.729: W/System.err(11971):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 22:02:40.729: W/System.err(11971):    at android.os.Looper.loop(Looper.java:137)
03-22 22:02:40.729: W/System.err(11971):    at android.app.ActivityThread.main(ActivityThread.java:5103)
03-22 22:02:40.729: W/System.err(11971):    at java.lang.reflect.Method.invokeNative(Native Method)
03-22 22:02:40.729: W/System.err(11971):    at java.lang.reflect.Method.invoke(Method.java:525)
03-22 22:02:40.729: W/System.err(11971):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-22 22:02:40.729: W/System.err(11971):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-22 22:02:40.729: W/System.err(11971):    at dalvik.system.NativeStart.main(Native Method)
03-22 22:02:40.819: D/AndroidRuntime(11971): Shutting down VM
03-22 22:02:40.819: W/dalvikvm(11971): threadid=1: thread exiting with uncaught exception (group=0x41a95700)
03-22 22:02:40.829: E/AndroidRuntime(11971): FATAL EXCEPTION: main
03-22 22:02:40.829: E/AndroidRuntime(11971): java.lang.RuntimeException: Unable to resume activity {com.example.simplergb/com.example.simplergb.SimpleRGB_Main}: java.lang.NullPointerException
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2790)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2266)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.os.Looper.loop(Looper.java:137)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.app.ActivityThread.main(ActivityThread.java:5103)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at java.lang.reflect.Method.invokeNative(Native Method)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at java.lang.reflect.Method.invoke(Method.java:525)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at dalvik.system.NativeStart.main(Native Method)
03-22 22:02:40.829: E/AndroidRuntime(11971): Caused by: java.lang.NullPointerException
03-22 22:02:40.829: E/AndroidRuntime(11971):    at com.example.simplergb.SimpleRGB_Main.onResume(SimpleRGB_Main.java:1364)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.app.Activity.performResume(Activity.java:5211)
03-22 22:02:40.829: E/AndroidRuntime(11971):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2780)
03-22 22:02:40.829: E/AndroidRuntime(11971):    ... 12 more

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    如果mBluetoothAdapter为空则取消操作; 我添加了返回:

    isBTConnected = false;
    return; // just cancel the operation 
    

        void findBT() {
        mmDevice = null;
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            msbox("BlueTooth ", "This device does not support bluetooth");
            isBTConnected = false;
            return; // just cancel the operation 
        } else {
    
            if (!mBluetoothAdapter.isEnabled()) {
                Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBluetooth, 0);
            }
    
        }
    
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
    
            for (BluetoothDevice device : pairedDevices) {
                // if(device.getName().equals("RN42-45A9"))
                if (device.getName().equals("RN42-45A9")) {
                    Log.d("TAG", "BT true");
                    mmDevice = device;
                    isBTConnected = true;
    
    
                } else {
    
                    isBTConnected = false;
                    Log.d("TAG", "BT false");
                    msbox("BlueTooth ", "You must pair device");
                }
    
            }
        } else {
            isBTConnected = false;
            msbox("BlueTooth ",
                    "You must pair this device with the Mobil-Link receiver before you can run application. Go to settings> Bluetooth> Pairing or see intruction manual for "
                            + "for pairing device");
        }
    }
    
    void openBT() throws IOException {
    
        //cancel if device is null
        if(mmDevice==null){
         retrurn;
        }
    
        UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // Standard
    
        mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
        mmSocket.connect();
        mmOutputStream = mmSocket.getOutputStream();
        mmInputStream = mmSocket.getInputStream();
    
        mConnectedThread = new ConnectedThread(mmSocket);
        mConnectedThread.start();
    
    }
    

    【讨论】:

    • 还是崩溃。 findBT() 似乎只查看 android 设备而不是目标接收器。有没有办法测试 openBT() 内部的连接?
    • check ` //如果设备为空则取消 if(mmDevice==null){ retrurn; }`
    • 仍然崩溃,mmDevice 不会为空,它从 android 设备的配对设备中获取名称。我需要检测接收器是否有效。不知道如何测试。我根据您的建议进行了更改,请参阅编辑。
    • 见附件 logcat。我不知道如何格式化以便于阅读
    • 任何人都对崩溃有任何想法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多