alexb
alexb

Reputation: 470

Pinging a bluetooth device in Android

Is there a way I can ping for a Bluetooth device in Android? The bluetooth device is not connected or paired in Android, but I know beforehand the MAC address and the PIN of the device. What I'm trying to achieve is to ping a list of MAC addresses to see if any of the devices are in range.

Upvotes: 2

Views: 8634

Answers (1)

alexb
alexb

Reputation: 470

Solved: What I did was query for the services available on the device (available UUIDs). If there's UUIDs received, then the device is in range.

So the steps were:

  • Register a broadcast received for the UUID action

    String action = "android.bluetooth.device.action.UUID";
    IntentFilter filter = new IntentFilter(action);
    registerReceiver(mReceiver, filter);
    
  • Create a bluetooth device based on the remote address and fetch it's UUIDs

    BluetoothDevice bd = bluetoothAdapter.getRemoteDevice(address);
    bd.fetchUuidsWithSdp(); 
    
  • Create a broadcast receiver, which carries the device address, being able to tell me that that

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
           //deviceExtra is our in range device
           deviceExtra  = 
           intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
           Parcelable[] uuidExtra = 
           intent.getParcelableArrayExtra("android.bluetooth.device.extra.UUID");
    }}};
    

Upvotes: 2

Related Questions