问题遇到的现象和发生背景
我目前在做android项目 :搜索蓝牙设备并通过列表显示 我用的是listview 使用的广播接收然后用listview的adapter 去添加设备信息 但是显示不出来
用代码块功能插入代码,请勿粘贴截图
private final BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
@SuppressLint("MissingPermission")
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG,"有回调");
switch (action) {
case BluetoothDevice.ACTION_FOUND:
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//已经匹配的设备
String str = device.getName() + "\n" + device.getAddress();
int deviceTypeImg = BtUtil.getDeviceType(device.getBluetoothClass( ) );
Log.d(TAG, "列表:" + str + " 图片:" + Long.toHexString(deviceTypeImg) );
if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
Log.d(TAG, "有匹配到设备");
bluetoothlist.add(new SPPView(deviceTypeImg, (str + "(已配对)") ) );
} else if (!bluetoothlist.contains(str)) {
Log.d(TAG, "有未匹配的设备");
bluetoothlist.add(new SPPView(deviceTypeImg, (str + "(未配对)") ) );
}
break;
case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
Log.d(TAG, "开始搜索的设备");
Toast.makeText(ScanSppFragment.this, "开始搜索", Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
Log.d(TAG, "搜索完毕");
Toast.makeText(ScanSppFragment.this, "搜索完毕", Toast.LENGTH_SHORT).show();
break;
}
mAdapter = new SPPAdapter(context,bluetoothlist);
listView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}
};
运行结果及报错内容
没有报错
我想要达到的结果
显示出来