在搜索蓝牙设备的过程中,我们如何获得唯一的android设备mac地址?

问题描述:

我只想要在设备聊天应用程序中搜索蓝牙设备时只包含手机和平板电脑的Android设备mac地址。在搜索蓝牙设备的过程中,我们如何获得唯一的android设备mac地址?

+0

什么码你尝试过?请告诉我们,我们可以帮助你。 – ChuongPham 2014-09-27 12:34:30

+0

@ChoungPham //从意图 \t \t \t \t BluetoothDevice类设备=意图 \t \t \t \t \t \t .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)的BluetoothDevice类对象; \t \t \t \t //如果它已经配对,跳过它,因为它已经上市 \t \t \t \t //已经 \t \t \t \t如果(device.getBondState()!= BluetoothDevice.BOND_BONDED){\t \t \t \t mNewDevicesArrayAdapter.add(device.getName()+ “\ n” 个 \t \t \t \t \t \t \t + device.getAddress()); \t \t \t \t}但它得到所有最近的蓝牙设备地址包括电话和标签 – 2014-09-27 13:05:24

+0

请参阅下面的答案。另外,您是否可以编辑自己的帖子,并在上面评论中列出代码,而不是将其发布到评论栏中。 – ChuongPham 2014-09-27 13:22:54

我想你已经知道得到一个列出的设备的Bluetooth MAC地址,但我会在这里列出来的完整性:

private static BluetoothAdapter getDeviceAdapter() { 
    final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    return bluetoothAdapter; 
} 

private static String getMacAddress() { 
    String macAddress = getDeviceAdapter().getAddress(); 
    return macAddress; 
} 

要确定Bluetooth设备是智能手机或平板电脑,请执行以下操作:

private static boolean isPhoneOrTablet(int deviceClass) { 
    // Tablets are defined as "COMPUTER_HANDHELD_PC_PDA" 
    // while smart phones are defined as "PHONE_SMART" 
    if ((deviceClass == BluetoothClass.Device.COMPUTER_HANDHELD_PC_PDA) 
     || (deviceClass == BluetoothClass.Device.PHONE_SMART)) { 
     return true; 
    } 
    return false; 
} 

根据​​方法的结果进行任何操作。 deviceClass参数来源于BluetoothDevice.getBluetoothClass().getDeviceClass()方法。

要同时选中多个Bluetooth发现的设备,使用一个循环,如:

for (BluetoothDevice device : devices) { 
    if (isPhoneOrTablet(device.getBluetoothClass().getDeviceClass())) { 
     Log.i("TESTING", getDeviceAdapter().getName()); 
    } 
} 
+0

是否可以将相同的消息逐个发送到多个设备,设备是否在队列中? – 2014-09-27 13:25:06

+0

参数设备类由davice.getBluetoothClass或任何其他变量设置。 – 2014-09-27 13:33:39

+0

@ user3848825:我更新了我的答案,以显示参数'deviceClass'从哪里来。 – ChuongPham 2014-09-27 13:43:08