80,355
社区成员
发帖
与我相关
我的任务
分享
<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>
<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"/>
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);
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;
}