蓝牙崩溃间歇性

时间:2015-08-22 18:18:36

标签: android bluetooth

我已经设置了蓝牙连接,并在蓝牙聊天中使用了一些代码。我发现启动时会出现间歇性崩溃。我认为它由于startActivityForResults而不是等待连接。我看到一些过去的线索已经解决了,但海报没有分享解决方案。

Android Bluetooth enable

我也无法弄清楚如何发布我的logcat信息,以便在线程中轻松阅读。我正在使用Android studio 1.3.1,我的选项是打印。

   void findBT() {
    mmDevice = null;
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        //msbox("BlueTooth ", "This device does not support bluetooth");
        isBTConnected = false;
        // Bluetooth tile indicator
        Resources res = getResources();

    } 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-4485")) {
                Log.d("TAG", "BT true");
                mmDevice = device;
                isBTConnected = true;
                // Bluetooth tile indicator
                Resources res = getResources();

            } else {
                // Bluetooth tile indicator
                isBTConnected = false;
                Log.d("TAG", "BT false");
                Resources res = getResources();
            }
        }
    } else {
        isBTConnected = false;
    }
}

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

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

    beginListenForData();

}

logcat的

08-22 13:32:13.781  11239-11314/com.example.jerry.els2015 W/dalvikvm﹕ threadid=12: thread exiting with uncaught exception (group=0x41879c08)
08-22 13:32:13.781  11239-11314/com.example.jerry.els2015 E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-502
    Process: com.example.jerry.els2015, PID: 11239
    java.lang.NullPointerException
            at com.example.jerry.els2015.MainActivity.GetData(MainActivity.java:1087)
            at com.example.jerry.els2015.MainActivity.access$300(MainActivity.java:44)
            at com.example.jerry.els2015.MainActivity$12.run(MainActivity.java:840)
            at java.lang.Thread.run(Thread.java:841)
08-22 13:32:13.861  11239-11239/com.example.jerry.els2015 D/GC﹕ <tid=11239> OES20 ===> GC Version   : GC Ver rls_pxa988_KK44_GC13.20
08-22 13:32:13.901  11239-11239/com.example.jerry.els2015 D/OpenGLRenderer﹕ Enabling debug mode 0
08-22 13:32:13.911  11239-11239/com.example.jerry.els2015 D/ProgressBar﹕ updateDrawableBounds: left = 0
08-22 13:32:13.911  11239-11239/com.example.jerry.els2015 D/ProgressBar﹕ updateDrawableBounds: top = 0
08-22 13:32:13.911  11239-11239/com.example.jerry.els2015 D/ProgressBar﹕ updateDrawableBounds: right = 308
08-22 13:32:13.911  11239-11239/com.example.jerry.els2015 D/ProgressBar﹕ updateDrawableBounds: bottom = 50
08-22 13:32:13.911  11239-11239/com.example.jerry.els2015 D/ProgressBar﹕ updateDrawableBounds: mProgressDrawable.setBounds()
08-22 13:37:14.334  11239-11314/? I/Process﹕ Sending signal. PID: 11239 SIG: 9

这是连接代码。

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

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

    beginListenForData();

}

这是getdata

    private void GetData(int FixtureNumber) {

    Log.d("TAG", "Value  " + intArrayToInt(intArray1));

    intByteCount=9;                         // set to receive 9 bytes

    byte buffer[] = new byte[6];

    buffer[0] = ((byte) 1); // Command (get data)
    buffer[1] = ((byte) Master_value);
    buffer[2] = ((byte) intArrayToInt(intArray1));
    buffer[3] = ((byte) intArrayToInt(intArray2));

    buffer[4] = ((byte) 3);
    buffer[5] = ((byte) 4);

    if (isBTConnected) {

        try {
            mmOutputStream.write(buffer);
        } catch (IOException e) {

            e.printStackTrace();
        }

    }
}

0 个答案:

没有答案