Android蓝牙 - MAC地址

时间:2015-05-13 12:07:01

标签: java android bluetooth android-bluetooth

当我点击列表视图中的设备时,我正试图获取蓝牙设备的MAC地址。我想传入

中的MAC地址

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

当我在listview上选择设备时,有没有办法提取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) {
            }
        }
    }
    }
}

2 个答案:

答案 0 :(得分:0)

BluetoothDevice

有获取mac地址getAddress REF

的方法

答案 1 :(得分:0)

在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();

要回答特定的案例,我假设您使用BluetoothChat1)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);
}