简体   繁体   English

如何在android中获取蓝牙连接设备的MAC地址

[英]how to get MAC Address of bluetooth connected device in android

I am sending an image via bluetooth in android and want to fetch the MAC address of Device to which the image is being sent.我正在通过 android 中的蓝牙发送图像,并希望获取图像被发送到的设备的 MAC 地址。

Please find below my code.请在我的代码下面找到。

private void bluetoothadd(){
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        // Device does not support Bluetooth

        Log.e("Bluetooth ","not found");
    }

    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivity(enableBtIntent);

        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        // If there are paired devices
        if (pairedDevices.size() > 0) {
            // Loop through paired devices
            for (BluetoothDevice device : pairedDevices) {


                Log.e("Mac Addressess","are:  "+mBluetoothAdapter.getRemoteDevice(device.getAddress()));
            }
            }
        }

}

I am getting all paired device's MAC Address.我正在获取所有配对设备的 MAC 地址。 I want the MAC Address of a device only to which data is being transmitted.我只想要数据正在传输到的设备的 MAC 地址。

用这个:

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
 private String connectedDeviceAdd = "";
 private BluetoothDevice connectedDevice;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    registerReceiver(this.mReceiver, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));




 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent
                    .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            connectedDeviceAdd = device.getAddress();
            connectedDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(connectedDeviceAdd);

        } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
            Log.e(TAG, "Device Disconnected");

        }
    }
};



 @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(this.mReceiver);
    }

When the intent is fired to connect to the remote device and the device is successfully established the Device Address is returned as extra data with the Flag EXTRA_DEVICE_ADDRESS .当意图连接到远程设备并成功建立设备时,设备地址将作为带有标志EXTRA_DEVICE_ADDRESS额外数据返回。

You can check for the connection and establish it您可以检查连接并建立它

if (!mBluetoothAdapter.isEnabled()) {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);

You can check the activity in the on onActivityResult function to find the address like this你可以在onActivityResult函数中查看活动,找到这样的地址

public void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode) {
        case REQUEST_CONNECT_DEVICE:
            // When DeviceListActivity returns with a device to connect
            if (resultCode == Activity.RESULT_OK) {
                // Get the device MAC address
                 String add = data.getExtras()
                                     .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
                address= add.toString();

                 // Get the BluetoothDevice object
                BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

            }
            break; 
}
}

This trick is used in the Bluetooth Chat Sample application you can find in the Examples folder of the SDK此技巧用于蓝牙聊天示例应用程序,您可以在 SDK 的示例文件夹中找到

So, it sounds like you want to get the bd_addr/mac of a device that you have a connection with?那么,听起来您想获取与您有连接的设备的 bd_addr/mac 吗? Then note that the BluetoothSocket class has a member 'getRemoteDevice', which returns a BluetoothDevice instance representing the device you are connected to, on which you can call getAddress() to get the MAC.然后请注意,BluetoothSocket 类有一个成员“getRemoteDevice”,它返回一个代表您所连接设备的 BluetoothDevice 实例,您可以在该实例上调用 getAddress() 来获取 MAC。

Or you can register for ACTION_ACL_CONNECTED which contains 'EXTRA_DEVICE' that will lead you to a BluetoothDevice.或者,您可以注册 ACTION_ACL_CONNECTED,其中包含“EXTRA_DEVICE”,它将引导您访问蓝牙设备。

这对我有用:

String macAddress = android.provider.Settings.Secure.getString(mContext.getContentResolver(), "bluetooth_address");

Try it.尝试一下。

WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
String address = info.getMacAddress();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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