我想在发现时过滤蓝牙设备,并且只保留同一软件开始监听的内容。例如,Android示例BluetoothChat,它应该只显示那些由BluetoothChat启动的蓝牙设备。
这是我的代码。
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...
......
}
}
};
}
答案 0 :(得分:0)
你可以做一件事:
使用软件启动蓝牙时,请将蓝牙名称更改为特定模式,以便识别设备是否由您的软件启动。
然后在搜索蓝牙设备时,检查您在搜索时遇到的蓝牙设备名称是否属于您定义的模式。
更改设备名称: