Android Bluetooth accept() / connect() with already paired devices

后端 未结 3 1152
执笔经年
执笔经年 2021-02-04 18:56

I am having trouble connecting two Android devices via Bluetooth, which happens only when they have been paired before. I am running one as the server and the other as the clien

3条回答
  •  情深已故
    2021-02-04 19:30

    In the client when I attempt to make a connection to a bonded device, I simply called it on the BluetoothDevice I found in BluetoothAdapter.getBondedDevices(). This does NOT work.

    In order to properly establish the Bluetooth connection, I had to do something similar to the pseudocode below:

    BluetoothDevice bonded = a device from BluetoothAdapter.getBondedDevices();
    BluetoothDevice actual = BluetoothAdapter.getRemoteDevice(bonded.getAddress());
    
    BluetoothSocket socket = actual.createRfcommSocketToServiceRecord(SOME_UUID);
    socket.connect();
    

    I arrived this answer by following exactly the Bluetooth chat example: Bluetooth Chat Service. Why it doesn't work on the device from getBondedDevices() is beyond me. Maybe someone with more intimate knowledge of Android can answer that.

提交回复
热议问题