简体   繁体   中英

how to get Bluetooth name of peer device in Android?

怎么可能获得Bluetooth的Android设备通过连接到对端设备的信息(名称,MAC地址,...), Bluetooth

You can use the following code to get all remote devices :)

private void discoveryDevice(){
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);

    registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy

    mBluetoothAdapter.startDiscovery();
}

// Create a BroadcastReceiver for ACTION_FOUND
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            Log.d(TAG, "device " + device.getName() + "\n" + device.getAddress());
        }
    }
};

It's really good for you to get the name of remote device throught the code:

Log.d(TAG, "device " + device.getName() + "\n" + device.getAddress());

I can't test this code at this moment but maybe can give an idea to how you can get the name and others parameters of bluetooth.

 static BluetoothSocket socket ;
 public static boolean btCon(Context ctx){

 Method m;
 try {
      BluetoothDevice devc = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MAC_ADRS);

      m = devc.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
      socket = (BluetoothSocket)m.invoke(devc, Integer.valueOf(1));
  socket.connect();

   String name = devc.getName();/* or */ String name2 = devc.getName().toString();
       Log.i("Test","Name: "+name); 
       Log.i("Test","Name 2: "+name2); 

  return true;
 }catch(Exception e)
 {
  return false;
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM