Android。如何将 UUID 信标值设置为正确的文本视图?
Android. How do I set UUID Beacon value to proper textview?
这是一个愚蠢的问题,但我有一个代码,它在 TextView 中为我提供了信标列表及其名称、地址、uuid、主要和次要。我正在计算 uuid、主要和次要值(在下面的代码中)。当我将这三个值设置到 TextView 中时,它们会与其他信标值交换。所以我的问题是,如何为正确的信标设置正确的 uuid、主要和次要值?
Link 举例:
http://i.stack.imgur.com/vlepI.jpg
我有两个信标。名称和地址是正确的,但如您所见,uuid、主要次要是相同的,并且在扫描过程中它们不断交换信标值。
代码如下。
DeviceScanActivity
public class DeviceScanActivity extends ListActivity {
private LeDeviceListAdapter mLeDeviceListAdapter;
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler;
private static final int REQUEST_ENABLE_BT = 1;
private static final long SCAN_PERIOD = 10000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle(R.string.title_devices);
mHandler = new Handler();
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
finish();
}
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
finish();
return;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_scan:
mLeDeviceListAdapter.clear();
scanLeDevice(true);
break;
case R.id.menu_stop:
scanLeDevice(false);
break;
}
return true;
}
@Override
protected void onResume() {
super.onResume();
if (!mBluetoothAdapter.isEnabled()) {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
mLeDeviceListAdapter = new LeDeviceListAdapter();
setListAdapter(mLeDeviceListAdapter);
scanLeDevice(true);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// User chose not to enable Bluetooth.
if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) {
finish();
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onPause() {
super.onPause();
scanLeDevice(false);
mLeDeviceListAdapter.clear();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
if (device == null) return;
//final Intent intent = new Intent(this, DeviceControlActivity.class);
if (mScanning) {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
mScanning = false;
}
//startActivity(intent);
}
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
invalidateOptionsMenu();
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
invalidateOptionsMenu();
}
private class LeDeviceListAdapter extends BaseAdapter {
private ArrayList<BluetoothDevice> mLeDevices;
private LayoutInflater mInflator;
public LeDeviceListAdapter() {
super();
mLeDevices = new ArrayList<BluetoothDevice>();
mInflator = DeviceScanActivity.this.getLayoutInflater();
}
public void addDevice(BluetoothDevice device) {
if(!mLeDevices.contains(device)) {
mLeDevices.add(device);
}
}
public BluetoothDevice getDevice(int position) {
return mLeDevices.get(position);
}
public void clear() {
mLeDevices.clear();
}
@Override
public int getCount() {
return mLeDevices.size();
}
@Override
public Object getItem(int i) {
return mLeDevices.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (view == null) {
view = mInflator.inflate(R.layout.listitem_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);
viewHolder.deviceMajor = (TextView) view.findViewById(R.id.device_major);
viewHolder.deviceMinor = (TextView) view.findViewById(R.id.device_minor);
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());
viewHolder.deviceUUID.setText(uuid);
viewHolder.deviceMajor.setText(major);
viewHolder.deviceMinor.setText(minor);
return view;
}
}
public String uuid;
public int major_temp;
public int minor_temp;
public String major;
public String minor;
public static final char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
int startByte = 2;
boolean patternFound = false;
while (startByte <= 5) {
if ( ((int) scanRecord[startByte + 2] & 0xff) == 0x02 && //Identifies an iBeacon
((int) scanRecord[startByte + 3] & 0xff) == 0x15) { //Identifies correct data length
patternFound = true;
break;
}
startByte++;
}
if (patternFound) {
byte[] uuidBytes = new byte[16];
System.arraycopy(scanRecord, startByte+4, uuidBytes, 0, 16);
String hexString = bytesToHex(uuidBytes);
//Here is your UUID
uuid = hexString.substring(0,8) + "-" +
hexString.substring(8,12) + "-" +
hexString.substring(12,16) + "-" +
hexString.substring(16,20) + "-" +
hexString.substring(20,32);
major_temp = (scanRecord[startByte+20] & 0xff) * 0x100 + (scanRecord[startByte+21] & 0xff);
major = Integer.toString(major_temp);
minor_temp = (scanRecord[startByte+22] & 0xff) * 0x100 + (scanRecord[startByte+23] & 0xff);
minor = Integer.toString(minor_temp);
// TextView textView1 = (TextView) findViewById(R.id.device_uuid);
// textView1.setText(uuid+" Major: " + major + " Minor: " + minor);
}
}
};
static class ViewHolder {
TextView deviceName;
TextView deviceAddress;
TextView deviceUUID;
TextView deviceMajor;
TextView deviceMinor;
}
}
和布局文件:
<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"/>
<TextView android:id="@+id/device_major"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12dp"/>
<TextView android:id="@+id/device_minor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12dp"/>
</LinearLayout>
您处理找到的信标的方式存在问题。只需仔细查看您为 UUID(主要和次要)分配任何值的位置以及 ListAdapter 的内容。
在您的 LeScanCallback
中,您将找到的任何蓝牙 LE 设备添加到列表适配器中。您将它们添加为 BluetoothDevice
,这只是一个通用模型,对 iBeacon 特定的 UUID、主要和次要一无所知。
然后将 UUID、major 和 minor 提取到 class 的成员变量中。这总是在找到的 BluetoothDevice
被识别为 iBeacon 时完成。在任何时候,您都只存储 一组 UUID,主要和次要。它们始终是找到的最新 iBeacon 的值。
因此,您可以创建一个简单的 class 来表示具有 UUID、主要、次要以及 RSSI 和 TxPower 的 iBeacon,而不是拥有一个 BluetoothDevices 列表,如果您想稍后进行任何距离估计。然后在扫描回调中不要将 BluetoothDevices 添加到列表中,而是在模式检查代码的末尾添加 iBeacons。类似于:
public BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
/* Remove:
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
*/
int startByte = 2;
boolean patternFound = false;
while (startByte <= 5) {
if (((int) scanRecord[startByte + 2] & 0xff) == 0x02 && //Identifies an iBeacon
((int) scanRecord[startByte + 3] & 0xff) == 0x15) { //Identifies correct data length
patternFound = true;
break;
}
startByte++;
}
if (patternFound) {
byte[] uuidBytes = new byte[16];
System.arraycopy(scanRecord, startByte + 4, uuidBytes, 0, 16);
String hexString = bytesToHex(uuidBytes);
//Here is your UUID
uuid = hexString.substring(0, 8) + "-" +
hexString.substring(8, 12) + "-" +
hexString.substring(12, 16) + "-" +
hexString.substring(16, 20) + "-" +
hexString.substring(20, 32);
major_temp = (scanRecord[startByte + 20] & 0xff) * 0x100 + (scanRecord[startByte + 21] & 0xff);
major = Integer.toString(major_temp);
minor_temp = (scanRecord[startByte + 22] & 0xff) * 0x100 + (scanRecord[startByte + 23] & 0xff);
minor = Integer.toString(minor_temp);
// Add:
IBeacon iBeacon = new IBeacon(uuid, major, minor);
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(iBeacon);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
}
}
然后更改您的列表适配器以使用 iBeacon 对象而不是 BluetoothDevice 对象并修复 getView() 中的代码。目前,您从上述成员变量中读取 UUID、major 和 minor,它们始终具有找到的最新 iBeacon 的值。
因此,改为从现在包含 iBeacon 对象的 mLeDevices 列表中读取值。类似于:
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (view == null) {
view = mInflator.inflate(R.layout.listitem_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);
viewHolder.deviceMajor = (TextView) view.findViewById(R.id.device_major);
viewHolder.deviceMinor = (TextView) view.findViewById(R.id.device_minor);
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());
// Change these:
viewHolder.deviceUUID.setText(device.getUuid);
viewHolder.deviceMajor.setText(device.getMajor);
viewHolder.deviceMinor.setText(device.getMinor);
return view;
}
然后您需要修改列表适配器的 addDevice 以比较 IBeacon 对象而不是 BluetoothDevice 对象。
public void addDevice(IBeacon device) {
// This probably won't without some extra work:
if(!mLeDevices.contains(device)) {
mLeDevices.add(device);
}
}
这是一个愚蠢的问题,但我有一个代码,它在 TextView 中为我提供了信标列表及其名称、地址、uuid、主要和次要。我正在计算 uuid、主要和次要值(在下面的代码中)。当我将这三个值设置到 TextView 中时,它们会与其他信标值交换。所以我的问题是,如何为正确的信标设置正确的 uuid、主要和次要值?
Link 举例: http://i.stack.imgur.com/vlepI.jpg
我有两个信标。名称和地址是正确的,但如您所见,uuid、主要次要是相同的,并且在扫描过程中它们不断交换信标值。
代码如下。
DeviceScanActivity
public class DeviceScanActivity extends ListActivity {
private LeDeviceListAdapter mLeDeviceListAdapter;
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler;
private static final int REQUEST_ENABLE_BT = 1;
private static final long SCAN_PERIOD = 10000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle(R.string.title_devices);
mHandler = new Handler();
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
finish();
}
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
finish();
return;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_scan:
mLeDeviceListAdapter.clear();
scanLeDevice(true);
break;
case R.id.menu_stop:
scanLeDevice(false);
break;
}
return true;
}
@Override
protected void onResume() {
super.onResume();
if (!mBluetoothAdapter.isEnabled()) {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
mLeDeviceListAdapter = new LeDeviceListAdapter();
setListAdapter(mLeDeviceListAdapter);
scanLeDevice(true);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// User chose not to enable Bluetooth.
if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) {
finish();
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onPause() {
super.onPause();
scanLeDevice(false);
mLeDeviceListAdapter.clear();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
if (device == null) return;
//final Intent intent = new Intent(this, DeviceControlActivity.class);
if (mScanning) {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
mScanning = false;
}
//startActivity(intent);
}
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
invalidateOptionsMenu();
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
invalidateOptionsMenu();
}
private class LeDeviceListAdapter extends BaseAdapter {
private ArrayList<BluetoothDevice> mLeDevices;
private LayoutInflater mInflator;
public LeDeviceListAdapter() {
super();
mLeDevices = new ArrayList<BluetoothDevice>();
mInflator = DeviceScanActivity.this.getLayoutInflater();
}
public void addDevice(BluetoothDevice device) {
if(!mLeDevices.contains(device)) {
mLeDevices.add(device);
}
}
public BluetoothDevice getDevice(int position) {
return mLeDevices.get(position);
}
public void clear() {
mLeDevices.clear();
}
@Override
public int getCount() {
return mLeDevices.size();
}
@Override
public Object getItem(int i) {
return mLeDevices.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (view == null) {
view = mInflator.inflate(R.layout.listitem_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);
viewHolder.deviceMajor = (TextView) view.findViewById(R.id.device_major);
viewHolder.deviceMinor = (TextView) view.findViewById(R.id.device_minor);
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());
viewHolder.deviceUUID.setText(uuid);
viewHolder.deviceMajor.setText(major);
viewHolder.deviceMinor.setText(minor);
return view;
}
}
public String uuid;
public int major_temp;
public int minor_temp;
public String major;
public String minor;
public static final char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
int startByte = 2;
boolean patternFound = false;
while (startByte <= 5) {
if ( ((int) scanRecord[startByte + 2] & 0xff) == 0x02 && //Identifies an iBeacon
((int) scanRecord[startByte + 3] & 0xff) == 0x15) { //Identifies correct data length
patternFound = true;
break;
}
startByte++;
}
if (patternFound) {
byte[] uuidBytes = new byte[16];
System.arraycopy(scanRecord, startByte+4, uuidBytes, 0, 16);
String hexString = bytesToHex(uuidBytes);
//Here is your UUID
uuid = hexString.substring(0,8) + "-" +
hexString.substring(8,12) + "-" +
hexString.substring(12,16) + "-" +
hexString.substring(16,20) + "-" +
hexString.substring(20,32);
major_temp = (scanRecord[startByte+20] & 0xff) * 0x100 + (scanRecord[startByte+21] & 0xff);
major = Integer.toString(major_temp);
minor_temp = (scanRecord[startByte+22] & 0xff) * 0x100 + (scanRecord[startByte+23] & 0xff);
minor = Integer.toString(minor_temp);
// TextView textView1 = (TextView) findViewById(R.id.device_uuid);
// textView1.setText(uuid+" Major: " + major + " Minor: " + minor);
}
}
};
static class ViewHolder {
TextView deviceName;
TextView deviceAddress;
TextView deviceUUID;
TextView deviceMajor;
TextView deviceMinor;
}
}
和布局文件:
<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"/>
<TextView android:id="@+id/device_major"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12dp"/>
<TextView android:id="@+id/device_minor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12dp"/>
</LinearLayout>
您处理找到的信标的方式存在问题。只需仔细查看您为 UUID(主要和次要)分配任何值的位置以及 ListAdapter 的内容。
在您的 LeScanCallback
中,您将找到的任何蓝牙 LE 设备添加到列表适配器中。您将它们添加为 BluetoothDevice
,这只是一个通用模型,对 iBeacon 特定的 UUID、主要和次要一无所知。
然后将 UUID、major 和 minor 提取到 class 的成员变量中。这总是在找到的 BluetoothDevice
被识别为 iBeacon 时完成。在任何时候,您都只存储 一组 UUID,主要和次要。它们始终是找到的最新 iBeacon 的值。
因此,您可以创建一个简单的 class 来表示具有 UUID、主要、次要以及 RSSI 和 TxPower 的 iBeacon,而不是拥有一个 BluetoothDevices 列表,如果您想稍后进行任何距离估计。然后在扫描回调中不要将 BluetoothDevices 添加到列表中,而是在模式检查代码的末尾添加 iBeacons。类似于:
public BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
/* Remove:
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
*/
int startByte = 2;
boolean patternFound = false;
while (startByte <= 5) {
if (((int) scanRecord[startByte + 2] & 0xff) == 0x02 && //Identifies an iBeacon
((int) scanRecord[startByte + 3] & 0xff) == 0x15) { //Identifies correct data length
patternFound = true;
break;
}
startByte++;
}
if (patternFound) {
byte[] uuidBytes = new byte[16];
System.arraycopy(scanRecord, startByte + 4, uuidBytes, 0, 16);
String hexString = bytesToHex(uuidBytes);
//Here is your UUID
uuid = hexString.substring(0, 8) + "-" +
hexString.substring(8, 12) + "-" +
hexString.substring(12, 16) + "-" +
hexString.substring(16, 20) + "-" +
hexString.substring(20, 32);
major_temp = (scanRecord[startByte + 20] & 0xff) * 0x100 + (scanRecord[startByte + 21] & 0xff);
major = Integer.toString(major_temp);
minor_temp = (scanRecord[startByte + 22] & 0xff) * 0x100 + (scanRecord[startByte + 23] & 0xff);
minor = Integer.toString(minor_temp);
// Add:
IBeacon iBeacon = new IBeacon(uuid, major, minor);
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(iBeacon);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
}
}
然后更改您的列表适配器以使用 iBeacon 对象而不是 BluetoothDevice 对象并修复 getView() 中的代码。目前,您从上述成员变量中读取 UUID、major 和 minor,它们始终具有找到的最新 iBeacon 的值。
因此,改为从现在包含 iBeacon 对象的 mLeDevices 列表中读取值。类似于:
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (view == null) {
view = mInflator.inflate(R.layout.listitem_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);
viewHolder.deviceMajor = (TextView) view.findViewById(R.id.device_major);
viewHolder.deviceMinor = (TextView) view.findViewById(R.id.device_minor);
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());
// Change these:
viewHolder.deviceUUID.setText(device.getUuid);
viewHolder.deviceMajor.setText(device.getMajor);
viewHolder.deviceMinor.setText(device.getMinor);
return view;
}
然后您需要修改列表适配器的 addDevice 以比较 IBeacon 对象而不是 BluetoothDevice 对象。
public void addDevice(IBeacon device) {
// This probably won't without some extra work:
if(!mLeDevices.contains(device)) {
mLeDevices.add(device);
}
}