7

我正在通过 android 中的蓝牙发送图像,并且想要获取图像发送到的设备的 MAC 地址。

请在下面找到我的代码。

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

}

我正在获取所有配对设备的 MAC 地址。我只想要数据传输到的设备的 MAC 地址。

4

6 回答 6

5

用这个:

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
于 2015-10-05T13:30:22.420 回答
3
 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);
    }
于 2019-08-05T05:52:52.887 回答
1

当意图连接到远程设备并且设备成功建立时,设备地址作为带有标志的额外数据返回EXTRA_DEVICE_ADDRESS

您可以检查连接并建立它

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

您可以检查 ononActivityResult函数中的活动以找到这样的地址

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; 
}
}

此技巧用于蓝牙聊天示例应用程序,您可以在 SDK 的示例文件夹中找到

于 2013-10-13T15:37:47.377 回答
0

那么,听起来您想获取与您有连接的设备的 bd_addr/mac 吗?然后请注意,BluetoothSocket 类有一个成员“getRemoteDevice”,它返回一个代表您连接到的设备的 BluetoothDevice 实例,您可以在该实例上调用 getAddress() 来获取 MAC。

或者您可以注册包含“EXTRA_DEVICE”的 ACTION_ACL_CONNECTED,它将引导您到蓝牙设备。

于 2013-10-11T16:48:25.603 回答
-1

这对我有用:

String macAddress = android.provider.Settings.Secure.getString(mContext.getContentResolver(), "bluetooth_address");
于 2016-12-12T13:05:08.380 回答
-3

试试看。

WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
String address = info.getMacAddress();
于 2013-10-11T10:35:03.917 回答