怎么在搜索蓝牙4.0 设备中调用蓝牙2.0的搜索界面?(和UI关系更大,和蓝牙4.0关系不大)

youyiyang 2016-10-05 09:42:59
你好!

本人主要是做个界面:上栏是新搜索的蓝牙4.0设备,下栏是已经配对的蓝牙4.0设备,下面还有个按钮可以手动搜索,
这个界面在2.0很多,但是蓝牙4.0在adatper里面是没有配对设备的记录的,是不是能用preference来记录?

因为我看很多demo里面都是一行一行的textview,然后程序里面使用ViewHolder来加载这些textview的(textview在xml里面写好了),要做到上面的界面,是2个ListView加一个Button,我不知道怎么在ViewHolder来调用ListView,只是知道ViewHolder是为了能加快调用界面,来加快显示搜索到的蓝牙设备。
比如,现在一般用textview的xml是这么写的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/device_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24dp"/>
<TextView android:id="@+id/device_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12dp"/>
<TextView android:id="@+id/device_uuid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12dp"/>
</LinearLayout>

我想做的界面的xml是这样写的:

<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/newDevice"/>
<ListView android:id="@+id/newDevices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stackFromBottom="true"
android:layout_weight="2">
</ListView>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/pairedDevice"/>
<ListView android:id="@+id/pairedDevices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stackFromBottom="true"
android:layout_weight="2">
</ListView>
<Button android:id="@+id/scanButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/scanNew"/>

蓝牙2.0调用上面这个界面一般是这么调用的:

super.onCreate(savedInstanceState);
// Setup the window
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.device_list);
bluetooth = BluetoothAdapter.getDefaultAdapter();
// Get a set of currently paired devices
Set<BluetoothDevice> pairedDevices = bluetooth.getBondedDevices();

scanButton = (Button)findViewById(R.id.scanButton);
scanButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
doDiscovery();
v.setVisibility(View.GONE);
}
});
newDevicesList = (ListView)findViewById(R.id.newDevices);
newDevicesAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
newDevices);
newDevicesList.setAdapter(newDevicesAdapter);
newDevicesList.setOnItemClickListener(mNewDeviceClickListener);

// Find and set up the ListView for paired devices
pairedDevicesList = (ListView)findViewById(R.id.pairedDevices);
pairedDevicesAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
pairedDevicesList.setAdapter(pairedDevicesAdapter);
//当点击时,启动线程connect
pairedDevicesList.setOnItemClickListener(mDeviceClickListener);

// 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);

现在ViewHolder是这么调用的:


public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
// General ListView optimization code.
if (view == null) {
view = mInflator.inflate(R.layout.list_item_device, null);
viewHolder = new ViewHolder();
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
viewHolder.deviceUuid = (TextView) view.findViewById(R.id.device_uuid);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}

BluetoothDevice device = mLeDevices.get(i);
final String deviceName = device.getName();
if (deviceName != null && deviceName.length() > 0)
viewHolder.deviceName.setText(deviceName);
else
viewHolder.deviceName.setText(R.string.unknown_device);
viewHolder.deviceAddress.setText(device.getAddress());
try{
//Method getUuidsMethod = BluetoothAdapter.class.getDeclaredMethod("getUuids", null);

ParcelUuid[] uuids = (ParcelUuid[]) device.getUuids();
String uuidStr="";
for (ParcelUuid uuid: uuids) {
uuidStr = uuid.getUuid().toString()+"\n";
Log.d("Log", "UUID: " + uuid.getUuid().toString());
}
viewHolder.deviceUuid.setText(uuidStr);
}catch(Exception e){
put(e.toString(),"mybleYYYError");
}
return view;
}
static class ViewHolder {
TextView deviceName;
TextView deviceAddress;
TextView deviceUuid;
}


其实我就是想在蓝牙4.0能调用这个界面就可以了

demo里面的搜索蓝牙4.0的界面一般是这样的:

这样一条的,没有已经配对的设备,还有一个按钮的

...全文
262 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
youyiyang 2016-10-07
  • 打赏
  • 举报
回复
好的,谢谢!我再试一下。
幻影宇寰 2016-10-06
  • 打赏
  • 举报
回复
我提供下思路,希望对你有所帮助! 你的第一个问题:4.0的adapter不能获取已经配对的设备: 你使用BluetoothAdapter.getDefaultAdapter()得到adapter再试试。 你的第二个问题,布局显示的问题: 你可以使用一个适配器实现设备的列表显示,至于需要显示两个不同的ListView,你可以在布局中添加两个ListView就行,只是需要改变ListView显示的数据就行,意思就是你在第一个ListView中装载的数据是正在搜索的设备列表,在第二个ListView中装载的数据是已经配对的设备列表。

80,355

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧