安卓蓝牙-MAC地址
Posted
技术标签:
【中文标题】安卓蓝牙-MAC地址【英文标题】:Android Bluetooth- MAC address 【发布时间】:2015-05-13 12:07:01 【问题描述】:当我单击列表视图中的设备时,我试图获取蓝牙设备的 MAC 地址。我想在
中传入 MAC 地址BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
当我在列表视图中选择一个设备时,有没有一种方法可以提取 MAC 地址。谢谢
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
if (mBluetoothAdapter.isDiscovering())
mBluetoothAdapter.cancelDiscovery();
if (listAdapter.getItem(i).contains("Paired"))
BluetoothDevice selectedDevice = devices.get(i);
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
mBluetoothAdapter.cancelDiscovery();
try
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
btSocket.connect();
catch (IOException e)
try
btSocket.close();
catch (IOException e2)
【问题讨论】:
【参考方案1】:在 android 中,启动蓝牙设备扫描后,我们会获得远程蓝牙设备的地址,作为我们注册的 BroadcastReceiver
上的广播消息。下面的代码示例就是这样做的。
//receive remote device details including address
BroadcastReceiver mReceiver = new BroadcastReceiver()
public void onReceive(Context context, Intent intent)
Sring 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);
String deviceAddress = device.getAddress());
//register receiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
//start bluetooth scan
BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
mBtAdapter.startDiscovery();
为了回答您的具体案例,我假设您正在使用BluetoothChat
(1) Android 示例代码在扫描后从配对设备创建 ListView。如果是这种情况,您可以在 Android 代码示例中获取设备地址(2)
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
String info = ((TextView) view).getText().toString();
String address = info.substring(info.length() - 17);
【讨论】:
【参考方案2】:BluetoothDevice
有方法获取mac地址getAddress
REF
【讨论】:
以上是关于安卓蓝牙-MAC地址的主要内容,如果未能解决你的问题,请参考以下文章