我使用方法bluetoothAdapter.getBondDevices()
获取所有配对设备的列表。
反正有没有检查有多少设备实际在线,即准备接收数据?
答案 0 :(得分:-1)
我在我的应用程序中正在做的是创建BroadcastReceiver,并实现onReceive(),我正在检查BluetoothDevice.ACTION_FOUND并将已发现的设备添加到列表中:
//holds names of BT devices discovered
private ArrayList<String> btDevices = new ArrayList<String>();
在onCreate()中,创建接收器:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getName() != null) {
btDevices.add(device.getName());
}
else {
Log.d("MyTag", "device.getName() is null");
}
}
}
};
然后,创建意图过滤器并注册接收器:
IntentFilter btFilter = new IntentFilter();
btFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
btFilter.addAction(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, btFilter);
获取bluetoothAdapter并开始发现:
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//we have adapter
if (bluetoothAdapter != null) {
//Bluetooth is OFF, so turn it on
if (!bluetoothAdapter.isEnabled()) {
bluetoothAdapter.enable();
}
else {
bluetoothAdapter.startDiscovery();
}
}
然后,我可以使用btDevices.size()
检查已发现的设备数量