Android Question java.io.IOException: [JSR82] - pairing android devices

merlin2049er

Well-Known Member
Licensed User
Longtime User
Hi, I've noticed that I get this exception while trying to pair two android devices.

I think the problem stems from them being previously paired. If I go to the settings, unpair the devices, and try establishing the connection it works fine.

Is there a way to remove the paired device from the os, and do a clean pairing within basic4android code?
 

merlin2049er

Well-Known Member
Licensed User
Longtime User
Hi.

B4X:
Sub Discover
    BtSerial.Disconnect
    foundDevices.Initialize
    foundDevices.Clear
    If BtAdmin.StartDiscovery    = False Then
        ToastMessageShow("Error starting discovery process.", True)
    Else
        ProgressDialogShow("Searching for devices...")
    End If   

End Sub

That's the discovery code from the one device.



This is the discovery "Listen code" on the other.

B4X:
Sub Listen_click
  Dim i As Intent
  i.Initialize("android.bluetooth.adapter.action.REQUEST_DISCOVERABLE", "")
  i.PutExtra("android.bluetooth.adapter.extra.DISCOVERABLE_DURATION", 300)
  StartActivity(i)
  BtSerial.Listen
End Sub


It makes the connection only when I haven't previously paired the two. If they have been paired, and the device is in the bluetooth settings, it will raise an exception in java.

Is it possible to unpair a device from the android OS, before attempting to pair it up again?
 
Upvote 0

merlin2049er

Well-Known Member
Licensed User
Longtime User
Ok, I found this on the android developers site...

Querying paired devices
Before performing device discovery, its worth querying the set of paired devices to see if the desired device is already known. To do so, call getBondedDevices(). This will return a Set of BluetoothDevices representing paired devices. For example, you can query all paired devices and then show the name of each device to the user, using an ArrayAdapter:

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();// If there are paired devicesif(pairedDevices.size()>0){
// Loop through paired devices
for(BluetoothDevice device : pairedDevices){
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName()+"\n"+ device.getAddress());
}}



So, if I've already have the device paired up. I just need to re-query it?


Is there something similar to getbondeddevices() ? within basic4android ?
 
Upvote 0

merlin2049er

Well-Known Member
Licensed User
Longtime User
Hmm, ok. Still having issues when the device has been previously paired.

I have to unpair it, and repair it to get it to work.
 
Upvote 0

mikimoto

Member
Licensed User
Longtime User
I had this Problem too.
Solution is disable Bluetooth and enable it again.

If admin.IsEnabled=True Then admin.Disable
 
Upvote 0
Top