Android中的蓝牙连接失败

时间:2014-07-08 08:12:00

标签: android sockets bluetooth android-bluetooth

连接蓝牙时出错

打开蓝牙设备时出错

我正在调用像这样的方法

findBT();
try {
    Thread.currentThread();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    openBT();
}

我的代码用于查找削减的蓝牙设备

void findBT () {
    try {
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        if (mBluetoothAdapter == null) {
            Toast.makeText(context, "  No bluetooth adapter available  ", Toast.LENGTH_LONG).show();
        }

        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) {

                // MP300 is the name of the bluetooth printer device
                if (device.getName().equals("AB-340M")) {
                    mmDevice = device;
                    break;
                }
            }
        }
        //Toast.makeText(context, "Found", Toast.LENGTH_LONG).show();
    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

我的开放蓝牙设备的Java代码

/*
 * Tries to open a connection to the bluetooth printer device
 */
void openBT ()throws IOException {
    try {
        // Standard SerialPortService ID
        UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
        mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
        mmSocket.connect();
        mmOutputStream = mmSocket.getOutputStream();
        mmInputStream = mmSocket.getInputStream();

        beginListenForData();

        //Toast.makeText(context, "OPEN", Toast.LENGTH_LONG).show();
    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

获取错误

07-08 13:22:44.710: W/BluetoothAdapter(9120): getBluetoothService() called with no BluetoothManagerCallback
07-08 13:22:44.937: W/System.err(9120): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
07-08 13:22:44.953: W/System.err(9120):     at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:1016)
07-08 13:22:44.953: W/System.err(9120):     at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:973)
07-08 13:22:44.953: W/System.err(9120):     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:662)
07-08 13:22:44.953: W/System.err(9120):     at com.clppl.project.SendChallan.openBT(SendChallan.java:343)
07-08 13:22:44.953: W/System.err(9120):     at com.clppl.project.SendChallan$ApproveAdapter$2.onClick(SendChallan.java:229)
07-08 13:22:44.953: W/System.err(9120):     at android.view.View.performClick(View.java:4452)
07-08 13:22:44.953: W/System.err(9120):     at android.widget.Button.performClick(Button.java:148)
07-08 13:22:44.953: W/System.err(9120):     at android.view.View$PerformClick.run(View.java:18428)
07-08 13:22:44.953: W/System.err(9120):     at android.os.Handler.handleCallback(Handler.java:725)
07-08 13:22:44.953: W/System.err(9120):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 13:22:44.953: W/System.err(9120):     at android.os.Looper.loop(Looper.java:176)
07-08 13:22:44.953: W/System.err(9120):     at android.app.ActivityThread.main(ActivityThread.java:5365)
07-08 13:22:44.953: W/System.err(9120):     at java.lang.reflect.Method.invokeNative(Native Method)
07-08 13:22:44.960: W/System.err(9120):     at java.lang.reflect.Method.invoke(Method.java:511)
07-08 13:22:44.960: W/System.err(9120):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-08 13:22:44.960: W/System.err(9120):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-08 13:22:44.960: W/System.err(9120):     at dalvik.system.NativeStart.main(Native Method)

请帮助我如何解决这个问题

先谢谢

1 个答案:

答案 0 :(得分:0)

我不确定但请检查您的设备Bluetooth版本是否为版本4.0 (Low Energy)。 Google已在Bluetooth LE API

中发布Android 4.3