首先我先翻译下插件里面提供的文档,主要了解下API:
1.public static BluetoothDeviceScript Initialize (bool asCentral, bool asPeripheral, Action action, Action<string> errorAction )
将蓝牙系统初始化为中心、外设或两者。作为一个外设只能在iOS上使用。完成动作回调时将执行。如果有错误errorAction回调将被执行。
2.public static void DeInitialize (Action action)
DeInitialize蓝牙系统。当完成动作回调时,将执行
3.public static void FinishDeInitialize ()
通知了其他所有东西都已被初始化
4.public static void PauseMessages (bool isPaused)
这个方法通知蓝牙系统该应用将会暂停或者停顿
这个方法将设备放入扫描模式中寻找任何外围设备支持serviceUUIDs参数数组中的UUIDs。如果serviceUUIDs是空的所有的蓝牙LE外设都将被发现。当发现设备的时候action方法会被调用,并传入外设的名字和ID
actionAdvertisingInfo 的默认值为空,如果赋值则每次从设备接收到advertising data时会被调用,你会接收到设备的地址/RSSI/制造商信息从advertising包中
6.public static void RetrieveListOfPeripheralsWithServices (string[] serviceUUIDs, Action<string, string> action)
这个方法会根据serviceUUIDs重新检索连接的外设列表,如果serviceUUIDs为空则所有的外设蓝牙都会被检索到,当发现设备的时候action方法会被调用,并传入外设的名字和ID
7.public static void StopScan ()
停止检索模式,在使用ScanForPeripheralsWithServices方法后调用
8.public static void ConnectToPeripheral (string name, Action<string> connectAction, Action<string, string> serviceAction, Action<string, string, string> characteristicAction,Action
这个方法尝试连接name命名的外设,如果连接成功connectAction方法会被调用(传入的参数为连接外设的名字),一旦连接serviceAction会被调用,会列出每个服务和每个服务支持的特征会被表示出来通过调用characteristicAction
disconnectAction的默认值为空的(对于向后兼容),如果你提供了一个回调连接外设失败的时候会被调用
9.public static void DisconnectPeripheral (string name, Action<string> action)
断开name外设的连接,完成后回调action
10.public static void ReadCharacteristic (string name, string service, string characteristic, Action<byte[]> action)
读取name外设的特征值,读取成功后回调action,byte[]为读取的数据
11.public static void WriteCharacteristic (string name, string service, string characteristic, byte[] data, int length, bool withResponse, Action<string> action)
通过外围设备的名称来启动一个特征的写入以及要写的服务和特性,数据,数据长度,是否需要回应。如果需要回应则写入后action会被调用
12.public static void SubscribeCharacteristic (string name, string service, string characteristic, Action<string> notificationAction, Action<string, byte[]> action)
根据name,service,characteristic订阅特征,当有通知的时候notificationAction会被调用,外设更新特征时action会被调用(第一个参数是特征UUID,第二个参数是特征更新的原始数据字节)这方法是为了向后兼容。带有设备地址的新方法在2.3版本中添加了(见下文)。
13.public static void SubscribeCharacteristicWithDeviceAddress (string name, string service, string characteristic, Action<string, string> notificationAction, Action
类似于12,只是action多了个参数,第一个参数为设备的地址,第二三个参数与12两个参数的意义相同
14.public static void UnSubscribeCharacteristic (string name, string service, string characteristic, Action<string> action)
取消订阅
忧伤,尝试使用发现插件不符合我的需求,因为我需要手机连接硬件,而初始化时一直不成功提示Bluetooth LE not Enable