我在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地址传输数据。
答案 0 :(得分:4)
使用此:
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
答案 1 :(得分:1)
当触发意图以连接到远程设备并且设备成功建立时,设备地址将作为带有标记EXTRA_DEVICE_ADDRESS
的额外数据返回。
您可以检查连接并建立连接
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
您可以查看on 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;
}
}
此技巧用于您可以在SDK的Examples文件夹中找到的蓝牙聊天示例应用程序
答案 2 :(得分:1)
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);
}
答案 3 :(得分:0)
所以,听起来你想要获得与你有连接的设备的bd_addr / mac?然后请注意,BluetoothSocket类有一个成员'getRemoteDevice',它返回一个代表你所连接设备的BluetoothDevice实例,你可以在其上调用getAddress()来获取MAC。
或者您可以注册ACTION_ACL_CONNECTED,其中包含“EXTRA_DEVICE”,可以引导您进入BluetoothDevice。
答案 4 :(得分:-1)
这项工作对我来说:
String macAddress = android.provider.Settings.Secure.getString(mContext.getContentResolver(), "bluetooth_address");
答案 5 :(得分:-3)
试试吧。
WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
String address = info.getMacAddress();