Which part of Bluechat sample program does pairing of discovered devices?

☆樱花仙子☆ 提交于 2019-12-23 01:28:41

问题


I need to send commands through an android phone to a Bluetooth enabled device (CSR8670)
I thought about using RFCOMM and I can reach the point of discovering the bluetooth device
After discovery I have used

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
device.createBond();  

to pair the Bluetooth chip with the android phone
Upto the pairing step seems to be working fine, as I can see that the chip is paired to the phone using the phone's builtin bluetooth settings interface, but I have no idea how to proceed after this as I seem to have hit a wall

The next thing I did was

serverSocketForCSRComm = myBluetoothAdapter.listenUsingRfcommWithServiceRecord("Hello world", MY_UUID);  

where MY_UUID has been defined as

private static final UUID MY_UUID = UUID.fromString("0000FFFF-0000-1000-8000-00805F9B34FB");  

in the MainActivity class. I should also point out that the documentation from CSR is not very clear so I need to work through this using a lot of hit and trial. I had used the arguments "Hello world" and MY_UUID above based on some comments in the CSR chip's sample code (I am open to changing these values as per needed; this specific UUID was chosen because the CSR comments say the device uses a uint16 UUID of 0xFFFF and I read here that this is how I should convert a 16-bit UUID to a proper 128-bit UUID). Also, listenUsingRfcommWithServiceRecord() funcition seems to return immediately no matter what I give as an argument, so a mismatched UUID might not be the sole cause of my problem.

After calling the above function, I call

btSocketForCSRComm = serverSocketForCSRComm.accept();  

When this function is called the app seems to get stuck. I believe that accept() is a blocking function and does not return until a proper connection to a BluetoothSocket is established which should be the reason for the app getting stuck. At this point I have hot a wall and so am trying to study the Bluechat sample program from android developer's website.

My question is, how exactly does pairing happen in the Bluechat program? I can see the function calls for discovery of new devices as well as getting a list of previously bonded devices, but I cannot find a call to createBond() function, and I am not sure which section of the code exactly is being used for pairing of the discovered devices. It does not help that I do not have two android devices with me right now, so I cannot even do a sample run of the Bluechat code to see it in action.

Please point me to the section of the code in the Bluechat program that does the actual pairing of the discovered devices, or please let me know the alternative way of pairing (by this point I am pretty confident it should exist, but am not able to find it) discovered devices besides using the createBond() function from the BluetoothDevice class.

EDIT1 (added for clarifying the comments)--

public ConnectThread(BluetoothDevice device) {
    Log.d("MYLOG", "ConnectThread(BluetoothDevice device) CONSTRUCTOR");
    mmDevice = device;
    BluetoothSocket tmp = null;

    try {
        Method method = device.getClass().getMethod("createBond", (Class[]) null);
        method.invoke(device, (Object[]) null);
    } catch (Exception e) {
        e.printStackTrace();
    }
    do{

    }while(device.getBondState()!=BluetoothDevice.BOND_BONDED);
    Log.d("MYLOG", "Bonding complete" );

    // Get a BluetoothSocket for a connection with the
    // given BluetoothDevice
    try {
        tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) {
        Log.d("MYLOG", "createRfcommSocketToServiceRecord(MY_UUID) GONE WRONG");
        Log.e(TAG, "create() failed", e);
    }
    mmSocket = tmp;
}

回答1:


This is not an answer, but a number of observations.

I tried the BluetoothChat example on my two Android phones, a MOTO G (master) and a Samsung Galaxy S III (slave). Before the test, the caches were cleared so that the two devices were not paired.

I have inserted some Logcat debugging messages to print the value of

BluetoothAdapter.getDefaultAdapter().getBondedDevices().size()

right before and after each call of the following methods:

  • listenUsingRfcommWithServiceRecord and listenUsingInsecureRfcommWithServiceRecord,
  • BluetoothServerSocket's accept(),
  • createRfcommSocketToServiceRecord and createInsecureRfcommSocketToServiceRecord,
  • BluetoothSocket's connect(), getInputStream() and getOutputStream(),
  • the read() and write() methods of the aforementioned data streams.

Surprisingly, during the course of chat, the reported value of getBondedDevices().size() was always zero. Furthermore, the Android API Guide says that

Once a connection is made with a remote device for the first time, a pairing request is automatically presented to the user.

...

Note: If the two devices have not been previously paired, then the Android framework will automatically show a pairing request notification or dialog to the user during the connection procedure

However, the dialog did not show up when I first run the BluetoothChat app. (It did show up on a subsequent run, but this time, connection failed.)

After the first run, I exited the app. Each device remained in the other's Android Bluetooth Settings "Available devices" list, but the two phones were not paired.

I also looked at the source code in the files containing in the Android SDK's Bluetooth folder. There are three methods whose names start with createBond, but only one of them is called, and called only once, in the file BluetoothTestUtils.java. Of course, that doesn't mean pairing is never done, but apparently, if pairing had ever occurred during the chat session, it was not performed by calling createBond directly.



来源:https://stackoverflow.com/questions/27685427/which-part-of-bluechat-sample-program-does-pairing-of-discovered-devices

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