Android bluetooth connect() throwing error

我只是一个虾纸丫 提交于 2019-12-10 19:32:36

问题


I have a project requirement wherein we have a product that contains two components -an android tablet and a PCB(containing an RN42 bluetooth chip).These two components are inside a plastic casing hence always will be in close proximity to each other.Both these devices would be paired once initially, hence the pairing info is present in the tablet forever.

Problem : In my code I make use of a function which initializes the bluetooth socket and gets access to the DataOut and DataIn ports using which the data can be exchanged between the tablet and pcb.

Case1: This piece of code has worked 100% of the time when I use Samsung S2(4.1.2) and the pcb,

Case2: BUT when I use my tablet(iball 3G 7271(4.1.2)) along with the pcb then most of the time I have issue while connecting to the socket.I have got errors stating-

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

on most occasions.

What I tried:

  • Tried using reflection ,but in vain.
  • Have used the generic UUID - 00001101-0000-1000-8000-00805F9B34FB,but no use.

I do not understand why the code works fine when I use samsung S2 with RN42 chip,but throws error when I use iball tablet with RN42 chip.On digging further I came across this post http://redacacia.me/2012/07/17/overcoming-android-bluetooth-blues-with-reflection-method/#comment-1414 according to which Chinese devices(iball being one) uses MTK processors,and there have been bluetooth connectivity issues in them.

Below is my code that does the bluetooth connection between the device and RN42 chip


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;
    }
}

Log for samsung s2(works fine all the time):

 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

Log for iball tablet using MKT familys processor:(one of the logcat errors):

 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

Please tell me :

  • What am I doing wrong/what should I do to resolve the connectivity issue of my iball tablet with the Rn42 chip.
  • Any useful resources that you can share.

Thanks in advance!

来源:https://stackoverflow.com/questions/22378331/android-bluetooth-connect-throwing-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!