cod*_*i14 11 uuid android bluetooth-lowenergy android-bluetooth arduino-uno
我正在尝试使用蓝牙低功耗(BLE)通过Android应用程序连接到Arduino Uno.
我正在开发Android Studio,使用三星Galaxy S4进行测试,使用Android版本5.0.1
我按照以下链接开发:http
://www.truiton.com/2015/04/android-bluetooth-low-energy- ble-example /
我正在扫描设备,当我找到它时,我想在连接它之前得到它的UUID,以确保它是正确类型的设备:
mScanCallback = new ScanCallback() {
@Override
@TargetApi(21)
public void onScanResult(int callbackType, ScanResult result) {
BluetoothDevice btDevice = result.getDevice();
ParcelUuid[] uuids = btDevice.getUuids(); //<-- this is always null!! :(
Log.d(TAG, ""+btDevice.fetchUuidsWithSdp()); //<-- prints true.
Log.d(TAG, "result : " + result.toString()); //<-- prints a bunch of relevant info that contains the UUID I want to check.
Log.d(TAG, "uuids : " + uuids); //<-- prints null.
/*
for (ParcelUuid u : uuids) {
//Compare with the UUID of my device, and connect if ok.
}
*/
}
Run Code Online (Sandbox Code Playgroud)
但是,btDevice.getUuids(); 总是返回null而没有错误...
如何获取扫描设备的UUID?
一个强力方法是使用regexp和result.toString()来抓住我想要的但是必须有更好的方法不是吗?
谢谢
BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner();
// scan for devices
scanner.startScan(new ScanCallback() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onScanResult(int callbackType, ScanResult result)
{
List<ParcelUuid> uuids = result.getScanRecord().getServiceUuids();
}
}
Run Code Online (Sandbox Code Playgroud)
这在Android 6.0.1中适用于我
来自 Java 文档:
公共 ParcelUuid[] getUuids ()
在 API 级别 15 中添加 返回远程设备支持的功能 (UUID)。
此方法不会启动服务发现过程来从远程设备检索 UUID。相反,会返回服务 UUID 的本地缓存副本。
如果需要新的 UUID,请使用 fetchUuidsWithSdp()。
需要蓝牙。
返回远程设备支持的功能 (UUID),如果出错则返回 null
您的 ScanResult 可能是一个错误
| 归档时间: |
|
| 查看次数: |
5293 次 |
| 最近记录: |