最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Android中连接蓝牙设备时遇到createRfcommSocketToServiceRecord的UUID问题和BluetoothSocket的connect失败

Bluetooth crifan 16408浏览 0评论

【问题】

折腾:

【记录】编写Android中的蓝牙模块驱动和底层HART设备

期间,参考:

Bluetooth | Android Developers – ManagingAConnection

参考“Connecting as a client”中的:

tmp = device.createRfcommSocketToServiceRecord(MY_UUID);

遇到UUID不懂的问题。

然后随便去

http://www.guidgenerator.com/online-guid-generator.aspx

弄了个UUID:

e214d9ae-c3ba-4e25-abb5-299041353bc3

结果运行到:

            try {
                // Connect the device through the socket. This will block
                // until it succeeds or throws an exception
                mmSocket.connect();
            } catch (IOException connectException) {
                // Unable to connect; close the socket and get out
                try {
                    mmSocket.close();
                } catch (IOException closeException) { }
                return;
            }

中的:

mmSocket.connect(); 

时就抛异常了。

即:

遇到createRfcommSocketToServiceRecord的UUID不懂,以及BluetoothSocket的connect失败。

 

【解决过程】

1.参考:

android – Why can’t HTC Droid running OTA 2.1 communicate with RFCOMM? – Stack Overflow

去试试:

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
mBTSocket = (BluetoothSocket) m.invoke(device, 1);

结果看着就不太对啊。。就不继续试了。

2.参考:

Need Bluetooth UUID clarification – Google Groups

去试试:

00001101-0000-100­0-8000-00805F9B34FB

结果直接挂掉。

3.再去试试:

00000003-0000-100­0-8000-00805F9B34FB

也会挂掉。

4.后来再去搜:

android bluetooth connect fail

然后去参考:

android bluetooth can’t connect – Stack Overflow

和:

The Missing Manual: Android Bluetooth RFCOMM « Wires Are Obsolete

去试试,用代码:

 // Create a BroadcastReceiver for ACTION_FOUND
 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
     public void onReceive(Context context, Intent intent) {
//	 BluetoothDevice deviceExtra = intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
//	 Parcelable[] uuidExtra = intent.getParcelableArrayExtra("android.bluetooth.device.extra.UUID");

         String action = intent.getAction();
         // When discovery finds a device
         if (BluetoothDevice.ACTION_FOUND.equals(action)) {
             // Get the BluetoothDevice object from the Intent
             BluetoothDevice btDev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
             Parcelable[] btDevUuid = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);

调试得到的btDevUuid都是null的。

5.换用:

         if (BluetoothDevice.ACTION_FOUND.equals(action)) {
             // Get the BluetoothDevice object from the Intent
             BluetoothDevice btDev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
             //Parcelable[] btDevUuid = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
             UUID btDevUuid = intent.getParcelableExtra(BluetoothDevice.EXTRA_UUID);

还是不行。

6.参考:

Issue 15919 – android – Bluetooth connect fails under 2.3.3 for SPP devices using secure comms

中提到的:

BluetoothDevice | Android Developers

中提示的:

Hint: If you are connecting to a Bluetooth serial board then try using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. However if you are connecting to an Android peer then please generate your own unique UUID.

所以再去试试这个UUID:

00001101-0000-1000-8000-00805F9B34FB

最终是可以了:

android bluetooth ssp uuid 00001101-0000-1000-8000-00805F9B34FB connect ok

对应的相关代码为:

 private String mactekHartModemName;
 private UUID mactekHartModemUuid;
    
 //void afterFoundBtHartModem(BluetoothDevice btDev, Parcelable[] btDevUuid){
 void afterFoundBtHartModem(BluetoothDevice btDev, UUID btDevUuid){
  if(null != btDevUuid){
      
  }
  
     //mactekHartModemName = btDev.getName(); //"MACTekViator75FE"
     //mactekHartModemUuid = UUID.fromString(mactekHartModemName);
  
  String uuidValue;
  //http://www.guidgenerator.com/online-guid-generator.aspx
  //uuidValue = "e214d9ae-c3ba-4e25-abb5-299041353bc3";
  
  //https://groups.google.com/forum/#!topic/android-developers/vyTEJOXELos
  //uuidValue = "00001101-0000-100­0-8000-00805F9B34FB";
  //uuidValue = "00000003-0000-100­0-8000-00805F9B34FB";
  uuidValue = "00001101-0000-1000-8000-00805F9B34FB";

  mactekHartModemUuid = UUID.fromString(uuidValue);

     ConnectThread connectBtThread = new ConnectThread(btDev);
     connectBtThread.start();
 }

    private class ConnectThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;
     
        public ConnectThread(BluetoothDevice device) {
            // Use a temporary object that is later assigned to mmSocket,
            // because mmSocket is final
            BluetoothSocket tmp = null;
            mmDevice = device;
     
            // Get a BluetoothSocket to connect with the given BluetoothDevice
            try {
                // MY_UUID is the app's UUID string, also used by the server code
                tmp = device.createRfcommSocketToServiceRecord(mactekHartModemUuid);//00001101-0000-1000-8000-00805F9B34FB

            } catch (IOException e) { }
            mmSocket = tmp;
        }
     
        public void run() {
            // Cancel discovery because it will slow down the connection
            mBluetoothAdapter.cancelDiscovery();
     
            try {
                // Connect the device through the socket. This will block
                // until it succeeds or throws an exception
                mmSocket.connect();
            } catch (IOException connectException) {
                // Unable to connect; close the socket and get out
                try {
                    mmSocket.close();
                } catch (IOException closeException) { }
                return;
            }
     
            // Do work to manage the connection (in a separate thread)
            manageConnectedSocket(mmSocket);
        }
     
        /** Will cancel an in-progress connection, and close the socket */
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) { }
        }
    }

 

【总结】

此处,必须使用Android的SSP(协议栈默认)的UUID:

00001101-0000-1000-8000-00805F9B34FB

才能正常和外部的,也是SSP串口的蓝牙设备去连接。

转载请注明:在路上 » 【已解决】Android中连接蓝牙设备时遇到createRfcommSocketToServiceRecord的UUID问题和BluetoothSocket的connect失败

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (7)

  1. 有些手机连不上
    katte8年前 (2016-07-13)回复
  2. 不是SSP, 而是SPP(Serial Port Profile)
    John10年前 (2015-02-18)回复
  3. 前面第2条用这个UUID说直接挂掉,后面第6条说这个UUID可以。
    后月10年前 (2015-01-30)回复
    • 我反复对比了成功的那个uuid和第二条那个uuid发现还真是一模一样的!博主真的不是在都我们么
      淑女9年前 (2015-07-30)回复
      • 嗯呢呗,后来我用了其他机子试了试,同样的UUID有的机子可以有的不行
        后月9年前 (2015-10-24)回复
        • 只用了一台手机测试,不知道竟然还有这样的情况呀~不过Android API上也说这个UUID是well-know(不知道怎样翻译准确)的。
          淑女9年前 (2015-11-03)回复
          • 我想知道为什么不试第一条,你怎么会知道它不对
            一路春合7年前 (2017-11-18)回复
94 queries in 0.204 seconds, using 22.13MB memory