我正在编写一个搜索多个BLE设备并将其添加到字符串数组的应用程序。发现完成后,它会触发片段活动,并为每个设备填充一个滑动层以控制设备。
我曾经使用HC-05蓝牙模块使用BluetoothAdapter.ACTION_DISCOVERY_FINISHED来捕获发现完成的消息,如下所示
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
bluetoothAdapter.cancelDiscovery();
snackBluetooth.dismiss();
doNotifyDataSetChangedOnce = true;
if(listSize>0){
logo.setVisibility(View.GONE);
//intro1.setVisibility(View.GONE);
//intro2.setVisibility(View.GONE);
numberOfPages = listSize;
SharedPreferences sharedData = context.getSharedPreferences(storedData,0);
SharedPreferences.Editor editor = sharedData.edit();
editor.putInt("NO_Fixtures", numberOfPages);
editor.commit();
setupPages();
}
对于LeScanCallback,正在发现设备,但只有在callBack()停止后,我才能找到一种调用setupPages的方法。
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String ss = device.getName();
if(ss!= null && ss.equals("MLT-BT05")) {
if(!Arrays.asList(bAddress).contains(device.getAddress())){
Toast.makeText(MainActivity.this, "Found a new Device", Toast.LENGTH_SHORT).show();
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
bName[Bindex] = device.getName();
bAddress[Bindex] = device.getAddress();
numOfDevices++;
addDevice(Bindex,numOfDevices,bName[Bindex],bAddress[Bindex]);
Bindex++;
}
}
if (ss!= null && ss.equals("LEDVANCE G50 F-6681")){
if(!Arrays.asList(bAddress).contains(device.getAddress())){
Toast.makeText(MainActivity.this, "Found a Second new Device", Toast.LENGTH_SHORT).show();
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
bName[Bindex] = device.getName();
bAddress[Bindex] = device.getAddress();
numOfDevices++;
addDevice(Bindex,numOfDevices,bName[Bindex],bAddress[Bindex]);
Bindex++;
}
}
}
});
}