Android 蓝牙- MAC 地址
Android Bluetooth- MAC address
当我在列表视图中单击设备时,我试图获取蓝牙设备的 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) {
}
}
}
}
}
BluetoothDevice
有获取 mac 地址的方法 getAddress
REF
在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 sample code to create a ListView from paired devices after the scan. If that is the case, you can get the device address as in the Android code sample(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);
}
当我在列表视图中单击设备时,我试图获取蓝牙设备的 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) {
}
}
}
}
}
BluetoothDevice
有获取 mac 地址的方法 getAddress
REF
在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 sample code to create a ListView from paired devices after the scan. If that is the case, you can get the device address as in the Android code sample(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);
}