我目前正试图通过扩展Git code,在本地脚本中实施BLE广告+扫描,并为此添加广播功能。在Android中,我已经成功构建了一个广告,我可以在iOS和Android下扫描和解析这个广告。
但是,我的iOS尝试目前无法将实际数据放入服务中:如果没有包含任何特征,我会收到错误*,这显然会转换为无效值,因此,当有广播时它不会包括服务。按照教程我发现*(在将其代码翻译成javascript样式后),我应该在CBMutableService上使用setCharacteristics
。但是,添加特征会导致setCharacteristics
不是可用函数的警告。
var kServiceUUID = NSString.stringWithString("2EACE71A-1F75-426E-833F-E77CB58F7201")
var kCharacteristicUUID = NSString.stringWithString("D40670C5-4ECC-4776-8661-B8A11C2833B3");
var serviceUUID = [CBUUID.UUIDWithString(kServiceUUID)];
var characteristicUUID = [CBUUID.UUIDWithString(kCharacteristicUUID)];
var message = arg.message || "No message configured.";
var service = CBMutableService.alloc().initWithTypePrimary(serviceUUID, true);
var service = CBMutableService.alloc().initWithTypePrimary(serviceUUID, true);
var characteristic = CBMutableCharacteristic.alloc().initWithTypePropertiesValuePermissions(characteristicUUID, CBCharacteristicPropertyRead | CBCharacteristicPropertyNotify, "test", CBAttributePermissionsReadable);
service.setCharacteristics([characteristic]);
Bluetooth._state.periManager.addService(service);
var dict = NSDictionary.alloc().initWithObjectsForKeys(["test2"],[kServiceUUID]);
Bluetooth._state.periManager.startAdvertising(dict);
我现在要
“CONSOLE LOG file:///app/main-view-model.js:199:32:TypeError:service.setCharacteristics不是函数。(在'service.setCharacteristics([characteristic])','service .setCharacteristics'未定义)“
作为错误。但是,不添加特征会给我
CBMutableService:0x17ef4bd0 Primary = YES,UUID = 2EACE71A-1F75-426E-833F-E77CB58F7201,Included Services =(null),Characteristics =(null)
作为服务但在scanresult中没有服务。
*我在iOS下编程并且目前正在使用Windows,因为我只能在工作时登录iOS。因此,我目前没有可用的链接。
答案 0 :(得分:0)
特征字段可以直接访问,但是特征本身需要正确,只需一个serviceUUID:
service.characteristics = [characteristic];
更新版本的代码(仍然不完整):
var kServiceUUID = NSString.stringWithString("2EACE71A-1F75-426E-833F-E77CB58F7201");
var kCharacteristicUUID = NSString.stringWithString("D40670C5-4ECC-4776-8661-B8A11C2833B3");
var serviceUUID = CBUUID.UUIDWithString(kServiceUUID);
var characteristicUUID = CBUUID.UUIDWithString(kCharacteristicUUID);
var service = CBMutableService.alloc().initWithTypePrimary(serviceUUID, true);
var characteristic = CBMutableCharacteristic.alloc().initWithTypePropertiesValuePermissions(characteristicUUID, CBCharacteristicPropertyRead | CBCharacteristicPropertyNotify, null, CBAttributePermissionsReadable);
service.characteristics = [characteristic];
此代码仍然不完整,因为该特征尚未具有值,但服务本身不会抱怨该特征。