没有蓝牙接收器关闭时,应用程序崩溃

时间:2014-03-23 01:31:18

标签: android bluetooth

我已经设置了蓝牙连接,除非目标接收器关闭,否则一切正常。 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

1 个答案:

答案 0 :(得分:0)

如果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();

}