android蓝牙连接name,Android蓝牙获得连接设备

如何获取Android的所有连接蓝牙设备列表,无论配置文件是什么?

但我想知道是否有更简单的方法来获取所有连接的蓝牙设备列表.

解决方法:

要查看完整列表,这是一个两步操作:

>获取当前配对设备的列表

>扫描或发现范围内的所有其他人

要获取当前配对设备的列表并进行迭代:

Set pairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();

if (pairedDevices.size() > 0) {

for (BluetoothDevice d: pairedDevices) {

String deviceName = d.getName();

String macAddress = d.getAddress();

Log.i(LOGTAG, "paired device: " + deviceName + " at " + macAddress);

// do what you need/want this these list items

}

}

发现是一个复杂的操作.为此,您需要告诉BluetoothAdapter开始扫描/发现.当它找到东西时,它会发送你需要通过BroadcastReceiver接收的Intents.

首先,我们将设置接收器:

private void setupBluetoothReceiver()

{

BroadcastRecevier btReceiver = new BroadcastReciver() {

@Override

public void onReceive(Context context, Intent intent) {

handleBtEvent(context, intent);

}

};

IntentFilter eventFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);

// this is not strictly necessary, but you may wish

// to know when the discovery cycle is done as well

eventFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

myContext.registerReceiver(btReceiver, eventFilter);

}

private void handleBtEvent(Context context, Intent intent)

{

String action = intent.getAction();

Log.d(LOGTAG, "action received: " + action);

if (BluetoothDevice.ACTION_FOUND.equals(action)) {

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

Log.i(LOGTAG, "found device: " + device.getName());

} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

Log.d(LOGTAG, "discovery complete");

}

}

现在剩下的就是告诉BluetoothAdapter开始扫描:

BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

// if already scanning ... cancel

if (btAdapter.isDiscovering()) {

btAdapter.cancelDiscovery();

}

btAdapter.startDiscovery();

标签:android,bluetooth,android-bluetooth,bluetooth-profile

来源: https://codeday.me/bug/20190702/1360296.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值