您的位置:首页 > 产品设计 > UI/UE

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

2017-01-03 05:14 525 查看
你好!

本人主要是做个界面:上栏是新搜索的蓝牙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能调用这个界面就可以了


<
4000
br />
demo里面的搜索蓝牙4.0的界面一般是这样的:



这样一条的,没有已经配对的设备,还有一个按钮的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: