安卓蓝牙API(3)

查找设备:
以下部分描述如何查找已经配对的设备或者发现附近的允许被发现的新设备。
一、列出已经配对的设备
使用getBondedDevices()方法,它将返回一些列已经配对好的蓝牙设备BluetoothDevices.
比如:

Set<BluetoothDevice>pairedDevices = mBluetoothAdapter.getBondedDevices();
    if(pairedDevices.size()>0){
        for(BluetoothDevice device :pairedDevices){
             mArrayAdapter.add(device.getName()+"\n" +device.getAddress());
    }
}

二、发现设备:
……
……
……
例如:

//Create a BroadcastReceiver for ACTION_FOUND
private final BroadcasetReceiver mReceiver = new BroadcastReceiver(){
public void onReceive(Context context,Intent intent){
String action = intent.getAction();
// When discovery finds a device
if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device = intent.getparcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mArrayAdapter.add(device.getName() + "\n" + device.geAddress());
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice. ACTION_FOUND);
registerReceiver(mReceiver,  filter); // Don't forget to unregister during onDestroy

Caution:
Performing device discovery is a heavy procedure for the Bluetooth adapter and will consume a lot of
its resources. Once you have found a device to connect, be certain that you always stop discovery with
cancelDiscovery() before attempting a connection. Also, if you already hold a connection with a device, then
performing discovery can significantly reduce the bandwidth available for the connection, so you should not
perform discovery while connected.

Enabling discoverability
…………………………………………………………………………………………

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值