Can't accept an incoming bluetooth connection unless device is discoverable

故事扮演 提交于 2020-01-14 06:19:19

问题


I've hit my wits end with this one.

I have a typical BluetoothServerSocket that I'm trying to get the A&D Weight scale (UC-321PBT) to connect to. Their company has an app (myFitnessCompanion) which doesn't seem to have many connection issues, yet the scale will only connect to my app if I have the phone 'discoverable'. Mind you this is after I pair with the scale.

The only key code that is really needed here is how the listener is open, beyond that it's typical Bluetooth

serverSocket = adapter.listenUsingRfcommWithServiceRecord("PWAccessP", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));

(I have also tried listenUsingInsecureRfcommWithServiceRecord and even reflection to open a port)

I interrogated my phones Bluetooth from another phone to list get the 'PWAccessP' value used by the myFitnessCompanion application, and even used the app to get the scale to pair with my phone.

The scale WILL enter into my socket listener when I make the phone discoverable, but other than that I see the BluetoothDevice begin making the low level connection monitoring the intent 'BluetoothDevice.ACTION_ACL_CONNECTED', but unless I have the phone in a discoverable mode it will not trigger my server socket's 'accept()' method. The pairing screen does not appear on new connections.

Any help would be appreciated. Considering I have to have the phone 'discoverable' for the connection to occur in my app vs the myFitnessCompanion allowing the connection at any time after pairing I can't see how the myFitnessCompanion is getting around this.

Thanks in advance.


回答1:


A and D device work as Master and the android device as the slave. So the android device must be in discoverable mode. Me tested with both listenUsingRfcommWithServiceRecord and listenUsingInsecureRfcommWithServiceRecord method.

            try {
                tmp = mBluetoothAdapter
                        .listenUsingInsecureRfcommWithServiceRecord(NAME,
                                MY_UUID); // #2
                // tmp.accept();
                // tmp =
                // mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME,
                // MY_UUID);
                // tmp =
                // mAdapter.getRemoteDevice("").createRfcommSocketToServiceRecord(MY_UUID);
                mServerSocket = tmp;
                mPortNumber = getPortNr();

            } catch (IOException e) {
                //Log.e("DROID", "listen() failed", e);
                flag=false;
                return flag;
            }


来源:https://stackoverflow.com/questions/13373328/cant-accept-an-incoming-bluetooth-connection-unless-device-is-discoverable

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