Android蓝牙connect()抛出错误

时间:2014-03-13 12:14:22

标签: java android bluetooth socketexception android-bluetooth

我有一个项目要求,其中我们的产品包含两个组件 - 安卓平板电脑和PCB(包含RN42蓝牙芯片)。这两个组件都在塑料外壳内部,因此总是彼此靠得很近。这些设备最初会配对一次,因此配对信息会永久存在于平板电脑中。

问题: 在我的代码中,我使用了一个函数来初始化蓝牙套接字并访问DataOut和DataIn端口,使用它们可以在平板电脑和pcb之间交换数据。

案例1:当我使用三星S2(4.1.2)和pcb时,这段代码已经100%有效,

案例2:但是当我使用我的平板电脑(iball 3G 7271(4.1.2))和pcb时,大多数时候我在连接插座时遇到问题。我有错误陈述 -

 -Service discovery failed,
 -Connection is not created (failed or aborted).
 -Resource is busy

在大多数情况下。

我尝试了什么:

  • 尝试使用反射,但徒劳无功。
  • 使用了通用的UUID - 00001101-0000-1000-8000-00805F9B34FB,但没有用。

我不明白为什么当我使用带有RN42芯片的三星S2时代码工作正常,但是当我使用带有RN42芯片的iball平板电脑时会抛出错误。进一步挖掘我发现这篇文章http://redacacia.me/2012/07/17/overcoming-android-bluetooth-blues-with-reflection-method/#comment-1414根据哪个中文设备(iball是其中之一)使用MTK处理器,其中存在蓝牙连接问题。

以下是我的设备和RN42芯片之间的蓝牙连接代码


public class AppFunctions {

    public static BluetoothAdapter mBluetoothAdapter;
    public static BluetoothAdapter iballAdapter = null;
    public static BluetoothSocket Socket = null;
    public static BluetoothDevice RN42_Device;
    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    private static String address = "00:06:66:49:57:60";    // MAC of RN42 bluetooth chip on PCB
    public static OutputStream DataOut = null;
    public static InputStream DataIn = null;

    //Function to turn on BT socket and to initialize it
    public static void setBluetooth(Context context) {

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        mBluetoothAdapter.disable(); //Disable bluetooth on device
        mBluetoothAdapter.enable();  //Enable bluetooth on device

        Timer mTimer = new Timer();
        mTimer.schedule(new TimerTask()
        {
            @Override
            public void run()
            {

                iballAdapter = BluetoothAdapter.getDefaultAdapter();
                Log.i("AppFuntions", "iballAdapter - " + iballAdapter);
                RN42_Device = iballAdapter.getRemoteDevice(address);
                Log.i("AppFuntions", "RN42_Device - " + RN42_Device);

                try {
                    Socket = RN42_Device.createRfcommSocketToServiceRecord(MY_UUID);

    /*              tried reflection,but no use
                    Method m = RN42_Device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
                    Socket = (BluetoothSocket) m.invoke(RN42_Device, 5);*/

                    resetConnection();
                    connect();

                    Log.i("AppFunctions", "DataOut -" + DataOut);
                    Log.i("AppFunctions", "DataIn -" + DataIn);
                } catch (Exception e) {e.printStackTrace();}

                }
        }, 10000);
    }

    private static void resetConnection() {
        if (DataIn != null) {
            try {DataIn.close();} catch (Exception e) {}
            DataIn = null;
        }

        if (DataOut != null) {
            try {DataOut.close();} catch (Exception e) {}
            DataOut = null;
        }

        if (Socket != null) {
            try {Socket.close();} catch (Exception e) {}
            Socket = null;
        }
    }

    private static boolean connect() {

        // Reset all streams and socket.
        resetConnection();

        if (RN42_Device == null)
            RN42_Device = iballAdapter.getRemoteDevice(address);

        // Make an RFCOMM binding.
        try {
            Socket = RN42_Device.createRfcommSocketToServiceRecord(MY_UUID);

/*          tried reflection,but no use
            Method m = RN42_Device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
            Socket = (BluetoothSocket) m.invoke(RN42_Device, 5);*/

        } catch (Exception e1) {e1.printStackTrace();return false;}

        try {

            Socket.connect(); // Fails here most of the time for iball tablet and RN42,though works always for samsung S2 and Rn42
        } catch (Exception e) {e.printStackTrace();return false;}

        try {
            DataOut = Socket.getOutputStream();
            DataIn = Socket.getInputStream();
        } catch (Exception e) {e.printStackTrace();return false;}

        return true;
    }
}

登录samsung s2(一直工作正常):

 I/AppFuntions(21103): iballAdapter - android.bluetooth.BluetoothAdapter@41fe9250
 I/AppFuntions(21103): RN42_Device - 00:06:66:49:57:60
 V/BluetoothSocket.cpp(21103): initSocketNative
 V/BluetoothSocket.cpp(21103): ...fd 66 created (RFCOMM, lm = 26)
 V/BluetoothSocket.cpp(21103): initSocketFromFdNative
 I/BluetoothConn(21103): connecting to Socket
 V/BluetoothSocket.cpp(21103): abortNative
 V/BluetoothSocket.cpp(21103): ...asocket_abort(53) complete
 V/BluetoothSocket.cpp(21103): destroyNative
 V/BluetoothSocket.cpp(21103): ...asocket_destroy(53) complete
 V/BluetoothSocket.cpp(21103): abortNative
 V/BluetoothSocket.cpp(21103): ...asocket_abort(66) complete
 V/BluetoothSocket.cpp(21103): destroyNative
 V/BluetoothSocket.cpp(21103): ...asocket_destroy(66) complete
 V/BluetoothSocket.cpp(21103): initSocketNative
 V/BluetoothSocket.cpp(21103): ...fd 53 created (RFCOMM, lm = 26)
 V/BluetoothSocket.cpp(21103): initSocketFromFdNative
 D/BluetoothUtils(21103): isSocketAllowedBySecurityPolicy start : device null
 V/BluetoothSocket.cpp(21103): connectNative
 V/BluetoothSocket.cpp(21103): ...connect(53, RFCOMM) = 0 (errno 115)
 I/AppFunctions(21103): DataOut -android.bluetooth.BluetoothOutputStream@41e8b688
 I/AppFunctions(21103): DataIn -android.bluetooth.BluetoothInputStream@41e95790

使用MKT系列处理器登录iball平板电脑:(其中一个logcat错误):

 I/AppFuntions(10598): iballAdapter - android.bluetooth.BluetoothAdapter@426ebb88
 I/AppFuntions(10598): RN42_Device - 00:06:66:49:57:60
 I/BluetoothSocket_MTK(10598): [JSR82] Bluetooth Socket Constructor
 I/BluetoothSocket_MTK(10598): [JSR82] type=1 fd=-1 auth=true encrypt=true port=-1
 I/BluetoothConn(10598): connecting to Socket
 I/BluetoothSocket_MTK(10598): [JSR82] close
 I/BluetoothSocket_MTK(10598): [JSR82] readLock got.
 I/BluetoothSocket_MTK(10598): [JSR82] Bluetooth Socket Constructor
 I/BluetoothSocket_MTK(10598): [JSR82] type=1 fd=-1 auth=true encrypt=true port=-1
 I/BluetoothSocket_MTK(10598): [JSR82] connect: do SDP
 I/BluetoothSocket_MTK(10598): [JSR82] SdpHelper::onRfcommChannelFound: channel=1
 I/BluetoothSocket_MTK(10598): [JSR82] connect: do SDP done; mPort=1
 W/System.err(10598): java.io.IOException: [JSR82] connect: Connection is not created (failed or aborted).
 W/System.err(10598):   at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:395)
 W/System.err(10598):   at com.example.guardmonitor.commons.AppFunctions.connect(AppFunctions.java:193)
 W/System.err(10598):   at com.example.guardmonitor.commons.AppFunctions.access$3(AppFunctions.java:176)
 W/System.err(10598):   at com.example.guardmonitor.commons.AppFunctions$1.run(AppFunctions.java:109)
 W/System.err(10598):   at java.util.Timer$TimerImpl.run(Timer.java:284)
 I/AppFunctions(10598): DataOut -null
 I/AppFunctions(10598): DataIn -null

请告诉我:

  • 我做错了什么/我该怎么做才能解决我的iball平板电脑与Rn42芯片的连接问题。
  • 您可以分享的任何有用资源。

提前致谢!

0 个答案:

没有答案