Android 发现时如何筛选Bluetooth设备
我想在发现BluetoothDevice时对其进行过滤,并只保留由同一软件开始侦听的设备。例如,Android示例BluetoothChat,它应该只显示由BluetoothChat启动的蓝牙设备 这是我的密码Android 发现时如何筛选Bluetooth设备,android,bluetooth,Android,Bluetooth,我想在发现BluetoothDevice时对其进行过滤,并只保留由同一软件开始侦听的设备。例如,Android示例BluetoothChat,它应该只显示由BluetoothChat启动的蓝牙设备 这是我的密码 public class DeviceListActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate
public class DeviceListActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Setup UI here
......
// Register for broadcasts when a device is discovered
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter);
// Register for broadcasts when discovery has finished
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, filter);
// Get the local Bluetooth adapter
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
// Request discover from BluetoothAdapter
mBtAdapter.startDiscovery();
}
// The BroadcastReceiver that listens for discovered devices and
// changes the title when discovery is finished
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// *** My problem is here. I got a BluetoothDevice now, but how can I
// *** check whether it is created by my software?
// *** if I can't, I have to add all of them to my list, and when user
// *** chooses it, connection will fail if it's not created by my software.
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
// When discovery is finished, change the Activity title
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
// Discovery is finished...
// wrap up code here...
......
}
}
};
}
你可以做一件事: 当您使用软件启动蓝牙时,请将蓝牙的名称更改为特定模式,以便您可以识别设备是否由软件启动 然后,在搜索蓝牙设备时,检查您在搜索时访问的蓝牙设备名称是否符合您定义的模式 要更改设备名称,请执行以下操作: