123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- export function uniAsyncPromise(name, options) {
- return new Promise((resolve, reject) => {
- uni[name]({
- ...(options || {}),
-
- success: (res) => {
- resolve(res);
- },
- fail: (err) => {
- reject(err);
- }
- });
- });
- }
- export function openBlue() {
- return uniAsyncPromise('openBluetoothAdapter')
- }
- export function startBluetoothDevicesDiscovery(option) {
- console.log('开始蓝牙扫描');
- uniAsyncPromise('startBluetoothDevicesDiscovery', option).then((res) => {
- console.log('正在搜寻蓝牙设备', res);
- });
- }
- export function getConnectedBluetoothDevices(option) {
- console.log('开始获取已连接设备');
- return uniAsyncPromise('getConnectedBluetoothDevices', option)
- }
- export function onfindBlueDevices(getDevices) {
-
- uni.onBluetoothDeviceFound((devices)=>{
-
- uniAsyncPromise('getBluetoothDevices').then((res) => {
- getDevices && getDevices(res.devices);
- });
- });
- }
- export function stopBlueDevicesDiscovery() {
-
- console.log('停止蓝牙扫描');
- return uniAsyncPromise('stopBluetoothDevicesDiscovery').then((res) => {
- console.log('停止搜寻蓝牙设备', res);
- });
- }
- export function createBLEConnection(deviceId, sucess, fail) {
-
- console.log('连接蓝牙设备', deviceId);
- uniAsyncPromise("createBLEConnection", {
- deviceId,
- timeout: 10000
- })
- .then(res => {
-
- stopBlueDevicesDiscovery();
- console.log('连接成功');
- sucess && sucess({
- res: res,
- });
- })
- .catch(res => {
- console.log('连接设备异常' + res);
- fail && fail({
- res: res,
- });
- })
-
- }
- export function closeBLEConnection(deviceId) {
- console.log('断开蓝牙设备', deviceId);
- uniAsyncPromise("closeBLEConnection", {
- deviceId
- })
- .then(res => {
- console.log('BLEDisconnect complete', res);
- })
- .catch(res => {
- console.log('断开设备异常' + res);
- })
-
- }
- export function getBLEDeviceServices(deviceId, success, fail) {
- console.log('获取ServiceId', deviceId);
-
- setTimeout(()=>{
- uniAsyncPromise("getBLEDeviceServices", {
- deviceId:deviceId
- })
- .then(res => {
- console.log('服务', res);
- success && success({
- serviceId: res.services,
- });
- })
- .catch((res) => {
-
- console.log('获取ServiceId异常' + res);
- fail && fail({
- res: res,
- });
- });
- },1000)
- }
- export function getDeviceCharacteristics(deviceId, services, success, fail) {
-
- console.log('获取Characteristics', deviceId, services);
- if (services.length) {
- const serviceId = services.shift().uuid;
- console.log('ServceID ', serviceId);
- uniAsyncPromise('getBLEDeviceCharacteristics', {
- deviceId,
- serviceId,
- })
- .then((res) => {
- console.log('getBLEDeviceCharacteristics', deviceId, serviceId, res);
- let finished = false;
- let write = false;
- let notify = false;
- let indicate = false;
- var readId;
- var writeId;
-
- for (var i = 0; i < res.characteristics.length; i++) {
- if (!notify) {
- notify = res.characteristics[i].properties.notify;
- if (notify) readId = res.characteristics[i].uuid;
- }
- if (!indicate) {
- indicate = res.characteristics[i].properties.indicate;
- if (indicate) readId = res.characteristics[i].uuid;
- }
- if (!write) {
- write = res.characteristics[i].properties.write;
- writeId = res.characteristics[i].uuid;
- }
- if ((notify || indicate) && write) {
-
- success &&
- success({
- serviceId,
- writeId: writeId,
- readId: readId,
- });
- finished = true;
- break;
- }
- }
- if (!finished) {
- getDeviceCharacteristics(deviceId, services, success, fail);
- }
- })
- .catch((res) => {
- getDeviceCharacteristics(deviceId, services, success, fail);
- });
- } else {
- fail && fail();
- }
- }
- export function onGetBLECharacteristicValueChange(options, onChange = function() {}) {
- console.log('deviceId ', options.deviceId);
- console.log('serviceId ', options.serviceId);
- console.log('characteristicId ', options.characteristicId);
- uniAsyncPromise('notifyBLECharacteristicValueChange', {
- state: true,
- ...options,
- }).then((res) => {
- console.log('onBLECharacteristicValueChange ');
- uni.onBLECharacteristicValueChange(onChange);
- });
- }
- export function senBlData(deviceId, serviceId, characteristicId,uint8Array,lasterSuccess,lasterError) {
- console.log('************deviceId = [' + deviceId + '] serviceId = [' + serviceId + '] characteristics=[' +characteristicId+ "]")
- var uint8Buf = Array.from(uint8Array);
- function split_array(datas,size){
- var result = {};
- var j = 0
- if(datas.length < size) {
- size = datas.length
- }
- for (var i = 0; i < datas.length; i += size) {
- result[j] = datas.slice(i, i + size)
- j++
- }
- return result
- }
- var sendloop = split_array(uint8Buf, 20);
- console.log(sendloop.length)
- function realWriteData(sendloop, i) {
- var data = sendloop[i]
- if(typeof(data) == "undefined"){
- lasterSuccess()
- return
- }
-
- var buffer = new ArrayBuffer(data.length)
- var dataView = new DataView(buffer)
- for (var j = 0; j < data.length; j++) {
- dataView.setUint8(j, data[j]);
- }
- uni.writeBLECharacteristicValue({
- deviceId,
- serviceId,
- characteristicId,
- value: buffer,
- success(res) {
- console.log('发送成功',i)
- setTimeout(()=>{
- realWriteData(sendloop, i + 1);
- },100)
- },
- fail(e) {
- console.log('发送数据失败',e)
- lasterError(e)
- }
- })
- }
- var i = 0;
- realWriteData(sendloop, i);
- }
- export function sendDataToDevice(options) {
- let byteLength = options.value.byteLength;
-
-
- const speed = plus.os.name != 'Android' ? 180 : 20;
-
- console.log("send data 20");
- console.log(options,byteLength,options.value);
- if (byteLength > 0) {
- uniAsyncPromise('writeBLECharacteristicValue', {
- ...options,
- value: options.value.slice(0, byteLength > speed ? speed : byteLength),
- })
- .then((res) => {
- if (byteLength > speed) {
- setTimeout(()=>{
- sendDataToDevice({
- ...options,
- value: options.value.slice(speed, byteLength),
- });
- },100)
- } else {
- options.lasterSuccess && options.lasterSuccess();
- }
- })
- .catch((res) => {
- console.log(res);
- options.lasterError && options.lasterError(res);
- });
- }
- }
- export function catchToast(err) {
- const errMsg = {
- 10000: '未初始化蓝牙模块',
- 10001: '蓝牙未打开,请打开蓝牙开关',
- 10002: '没有找到指定设备',
- 10003: '连接失败',
- 10004: '没有找到指定服务',
- 10005: '没有找到指定特征值',
- 10006: '当前连接已断开',
- 10007: '当前特征值不支持此操作',
- 10008: '系统上报异常',
- 10009: '系统版本低于 4.3 不支持BLE',
- 10012: '连接超时,请重试'
- };
- let coode = err.errCode ? err.errCode.toString() : '';
- let msg = errMsg[coode];
-
- plus.nativeUI.toast(msg || coode, {
- align: 'center',
- verticalAlign: 'center',
- duration:'long'
- });
-
- }
|