查找设备:
以下部分描述如何查找已经配对的设备或者发现附近的允许被发现的新设备。
一、列出已经配对的设备
使用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
…………………………………………………………………………………………