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.

share|improve this question
    
I don't think the MAC address is applicable to bluetooth. General hardware addresses seem to be though. I'm just thinking out loud now. – keyser Dec 9 '13 at 21:45
    
What you're describing sounds a lot like Bluetooth discovery. When you receive info about discoverable devices, you get the name and MAC address. If you know the MAC address ahead of time, then you can try to connect to it, but there is a delay in doing that with each device. – BitBank Dec 9 '13 at 22:57
    
I was thinking of a faster way than connecting to it, like a simple ping. – alexb Dec 10 '13 at 0:39
up vote 3 down vote accepted

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");
    }}};
    
share|improve this answer
1  
I tried this but i am getting uuids even for devices that are not online but paired – Calvin Sep 3 '15 at 10:16
1  
This won't work, as Calvin noticed. If the connection fails, cached uuids are sent with the intent. developer.android.com/reference/android/bluetooth/… – Tore Rudberg Feb 4 '16 at 9:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.