蓝牙发现同一个设备 10 次
Bluetooth discovering the same device 10 times
我正在与 Android studio 一起开发蓝牙应用程序。今天,我遇到一个奇怪的问题。
我有 3 台蓝牙设备、一部智能手机、一台平板电脑和另一台设备,但我们并不关心它。
我在两台设备上执行相同的应用程序,但智能手机是 Android 8.1 (API 27) 平板电脑是 Android 4.0.4 (API 15).
在智能手机上,该应用程序运行良好。当我扫描附近的设备时,我得到了 4 个不同的设备。
但是问题来了。在平板电脑上,当我扫描附近的设备时,智能手机检测到的每种设备几乎是我的 10 倍。我真的不知道为什么这两个设备没有发现彼此。也许 Android 版本是该错误的原因。
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) { // discover devices
Scanned_devices = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
scanned_deviceName = Scanned_devices.getName();
scanned_macAddress = Scanned_devices.getAddress();
mDeviceList.add(scanned_deviceName + "\n" + scanned_macAddress);
Log.i("BT", scanned_deviceName + "\n" + scanned_macAddress);
Set<BluetoothDevice> pairedDevices = blueAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
try {
if (scanned_deviceName.equals(device.getName()) || scanned_macAddress.equals(device.getAddress())) {
Toast.makeText(getApplicationContext(), "Already Paired", Toast.LENGTH_LONG).show();
mDeviceList.remove(scanned_deviceName + "\n" + scanned_macAddress);
} //else {
//mDeviceList.add(scanned_deviceName + "\n" + scanned_macAddress);
//Log.i("BT", scanned_deviceName + "\n" + scanned_macAddress);
//}
}catch(Exception e)
{
Log.d("tag", "not working");
Toast.makeText(getApplicationContext(), "not working..", Toast.LENGTH_LONG).show();
}
}
}
Scanned_devices_ListView.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, mDeviceList));
}
}
};
在尝试了很多方法来检测设备是否已经存在于我的 ArrayList 中之后,我尝试了这段代码:
if (!mDeviceList.contains(scanned_deviceName + "\n" + scanned_macAddress))
{
mDeviceList.add(scanned_deviceName + "\n" + scanned_macAddress);
Log.i("BT", scanned_deviceName + "\n" + scanned_macAddress);
}
这将检查数组是否包含此字符串:
DeviceName
DeviceMac@
如果条件为真,则将字符串添加到 ArrayList。
如果为false(ArrayList中存在另一个内容相同的String),则该String不添加到ArrayList中。
我正在与 Android studio 一起开发蓝牙应用程序。今天,我遇到一个奇怪的问题。 我有 3 台蓝牙设备、一部智能手机、一台平板电脑和另一台设备,但我们并不关心它。
我在两台设备上执行相同的应用程序,但智能手机是 Android 8.1 (API 27) 平板电脑是 Android 4.0.4 (API 15). 在智能手机上,该应用程序运行良好。当我扫描附近的设备时,我得到了 4 个不同的设备。
但是问题来了。在平板电脑上,当我扫描附近的设备时,智能手机检测到的每种设备几乎是我的 10 倍。我真的不知道为什么这两个设备没有发现彼此。也许 Android 版本是该错误的原因。
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) { // discover devices
Scanned_devices = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
scanned_deviceName = Scanned_devices.getName();
scanned_macAddress = Scanned_devices.getAddress();
mDeviceList.add(scanned_deviceName + "\n" + scanned_macAddress);
Log.i("BT", scanned_deviceName + "\n" + scanned_macAddress);
Set<BluetoothDevice> pairedDevices = blueAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
try {
if (scanned_deviceName.equals(device.getName()) || scanned_macAddress.equals(device.getAddress())) {
Toast.makeText(getApplicationContext(), "Already Paired", Toast.LENGTH_LONG).show();
mDeviceList.remove(scanned_deviceName + "\n" + scanned_macAddress);
} //else {
//mDeviceList.add(scanned_deviceName + "\n" + scanned_macAddress);
//Log.i("BT", scanned_deviceName + "\n" + scanned_macAddress);
//}
}catch(Exception e)
{
Log.d("tag", "not working");
Toast.makeText(getApplicationContext(), "not working..", Toast.LENGTH_LONG).show();
}
}
}
Scanned_devices_ListView.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, mDeviceList));
}
}
};
在尝试了很多方法来检测设备是否已经存在于我的 ArrayList 中之后,我尝试了这段代码:
if (!mDeviceList.contains(scanned_deviceName + "\n" + scanned_macAddress))
{
mDeviceList.add(scanned_deviceName + "\n" + scanned_macAddress);
Log.i("BT", scanned_deviceName + "\n" + scanned_macAddress);
}
这将检查数组是否包含此字符串:
DeviceName
DeviceMac@
如果条件为真,则将字符串添加到 ArrayList。
如果为false(ArrayList中存在另一个内容相同的String),则该String不添加到ArrayList中。