欢迎来到258分享网,纯净的网络源码分享基地!

258资源分享网

全部作品
全部作品
网站源码
微信源码
素材特效
源码插件
视频教程
建站学院
热门搜索: 织梦  农业种植  农业  安全设置  官方
258资源分享 > 建站学院 > 微信开发 > 熊晨沣蓝牙实战---小程序蓝牙连接的开发1.0

推荐下载

HTML5响应式自适应网咯设计

2020-05-12   浏览:740

高端HTML5响应式企业通用网

2020-05-06   浏览:521

html5响应式外贸网站英文版

2020-05-08   浏览:510

HTML5自适应律师工作室类网

2020-04-04   浏览:504

HTML5影视传媒文化公司类网

2020-05-12   浏览:500

熊晨沣蓝牙实战---小程序蓝牙连接的开发1.0

发布时间:2020-12-28  

微信小程序蓝牙连接2.0说明:

1、本版本区分了ANDROID和IOS系统下蓝牙连接的不同方式。 
2、兼容了更多情况下的链接包括:

(1)未开启设备蓝牙,当监听到开启了蓝牙后自动开始连接。 
(2)初始化蓝牙失败后每3000ms自动重新初始化蓝牙适配器。 
(3)安卓端开启蓝牙适配器扫描失败,每3000ms自动重新开启。 
(4)IOS端获取已连接蓝牙设备为空,每3000ms自动重新获取。 
(5)安卓端蓝牙开始链接后中断扫描,连接失败了,重新开始扫描。 
(6)IOS端开始连接设备后,停止获取已连接设备,连接失败自动重新开启获取。 
(7)连接成功后,关闭系统蓝牙,蓝牙适配器重置。 
(8)连接成功后,关闭系统蓝牙,再次打开蓝牙,自动重新开始连接。 
(9)连接成功后,关闭目标蓝牙设备,自动重新开始扫描(获取)。 
(10)连接成功后,最小化小程序(连接未中断),打开小程序显示已连接。 
(11)连接成功后,杀掉小程序进程,连接关闭,自动重新开始扫描(获取)。

3、想起来了再来更新....。 
4、流程图,明天或后天或...谁有空帮我画一下也行。

我的连接是在App.js中做的。 
在App.js中的onLaunch触发是调用 init()方法。 
init代码:

 

init: function (n) {

 

this.list = [];

 

this.serviceId = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E";

 

this.serviceId_2 = "00001803-0000-1000-8000-00805F9B34FB";

 

this.serviceId_3 = "00001814-0000-1000-8000-00805F9B34FB";

 

this.serviceId_4 = "00001802-0000-1000-8000-00805F9B34FB";

 

this.serviceId_5 = "00001804-0000-1000-8000-00805F9B34FB";

 

this.serviceId_6 = "00001535-1212-EFDE-1523-785FEABCD123";

 

this.characterId_write = "6E400042-B5A3-F393-E0A9-E50E24DCCA9E";

 

this.characterId_read = "6E400012-B5A3-F393-E0A9-E50E24DCCA9E";

 

this.connectDeviceIndex = 0;

 

this.isGettingConnected = false;

 

this.isDiscovering = false;

 

this.isConnecting = false;

 

this.connectedDevice = {};

 

console.log('init state', this.connectedDevice.state);

 

if (!this.connectedDevice.state || n == 200) {

 

this.connectedDevice.state = false;

 

this.connectedDevice.deviceId = '';

 

this.adapterHasInit = false

 

}

 

this.startConnect();

 

}

 

说明:

1、 serviceId_2~6 是我已知的想要连接的蓝牙设备的serviceId可以只写一个。  2、characterId_write 是我已知的想要连接的蓝牙设备写入数据的特征值。  3、characterId_read是我已知的想要连接的蓝牙设备读取数据的特征值。  (以上3个都是为了做比对,真实的操作按照获取到的sericeid, characterid为准)。  4、connectedDevice 是已连接了的设备信息对象。

init完成后开始调用连接 startConnect();

 

startConnect代码:

 

startConnect: function () {

 

var that = this;

 

if (that.connectedDevice.state) return;

 

that.connectedDevice.deviceId = "";

 

that.connectedDevice.state = false;

 

// 如果适配器已经初始化不在调用初始化(重复初始化会报错)

 

if (this.adapterHasInit == undefined || this.adapterHasInit) return;

 

wx.showLoading({

 

title: '初始化蓝牙',

 

duration: 2000

 

});

 

// 开启蓝牙适配器状态监听

 

this.listenAdapterStateChange();

 

// 初始化蓝牙适配器状态(必须步骤,否则无法进行后续的任何操作)

 

wx.openBluetoothAdapter({

 

success: function (res) {

 

console.log("初始化蓝牙适配器成功");

 

that.getBluetoothAdapterState();

 

that.adapterHasInit = true;

 

},

 

fail: function (err) {

 

console.log(err);

 

wx.showLoading({

 

title: '请开蓝牙',

 

icon: 'loading',

 

duration: 2000

 

})

 

}

 

});

 

}

说明:这段有注释,就不多说了,比较简单。

在初始化蓝牙适配器状态成功后调用getBluetoothAdapterState()方法。

 

getBluetoothAdapterState代码:

 

getBluetoothAdapterState: function () {

 

var that = this;

 

wx.getBluetoothAdapterState({

 

success: function (res) {

 

console.log(res);

 

var available = res.available;

 

that.isDiscovering = res.discovering;

 

if (!available) {

 

wx.showLoading({

 

title: '请开蓝牙',

 

icon: 'loading',

 

duration: 2000

 

})

 

} else {

 

if (!that.connectedDevice['state']) {

 

that.judegIfDiscovering(res.discovering);

 

}

 

}

 

},

 

fail: function (err) {

 

console.log(err);

 

}

 

})

 

}

说明:此方法是用来获取当前蓝牙状态。

当检测到蓝牙可用时调用judegIfDiscovering方法。

 

judegIfDiscovering代码:

 

judegIfDiscovering: function (discovering) {

 

var that = this;

 

if (this.isConnectinng) return;

 

wx.getConnectedBluetoothDevices({

 

services: [that.serviceId],

 

success: function (res) {

 

console.log("获取处于连接状态的设备", res);

 

var devices = res['devices'];

 

if (devices[0]) {

 

if (that.isAndroidPlatform) {

 

wx.showToast({

 

title: '蓝牙连接成功',

 

icon: 'success',

 

duration: 2000

 

});

 

} else {

 

that.getConnectedBluetoothDevices(256);

 

}

 

} else {

 

if (discovering) {

 

wx.showLoading({

 

title: '蓝牙搜索中'

 

})

 

} else {

 

if (that.isAndroidPlatform) {

 

that.startBluetoothDevicesDiscovery();

 

} else {

 

that.getConnectedBluetoothDevices(267);

 

}

 

}

 

}

 

},

 

fail: function (err) {

 

console.log('getConnectedBluetoothDevices err 264', err);

 

if (that.isAndroidPlatform) {

 

that.startBluetoothDevicesDiscovery();

 

} else {

 

that.getConnectedBluetoothDevices(277);

 

}

 

}

 

});

 

}

说明:  1、此方法是用来判断是否正在扫描。  2、isAndroidPlatform 是通过小程序的getSystemInfo获取到的判断是安卓设备还是IOS设备。

如果是安卓设备调用startBluetoothDevicesDiscovery()开启扫描,如果是IOS设备调用getConnectedBluetoothDevices() 开启获取已配对的蓝牙设备。

 

startBluetoothDevicesDiscovery代码:

 

startBluetoothDevicesDiscovery: function () {

 

var that = this;

 

if (!this.isAndroidPlatform) return;

 

if (!this.connectedDevice['state']) {

 

wx.getBluetoothAdapterState({

 

success: function (res) {

 

console.log(res);

 

var available = res.available;

 

that.isDiscovering = res.discovering;

 

if (!available) {

 

wx.showLoading({

 

title: '请开蓝牙',

 

icon: 'loading',

 

duration: 2000

 

})

 

} else {

 

if (res.discovering) {

 

wx.showLoading({

 

title: '蓝牙搜索中'

 

})

 

} else {

 

wx.startBluetoothDevicesDiscovery({

 

services: [],

 

allowDuplicatesKey: true,

 

success: function (res) {

 

that.onBluetoothDeviceFound();

 

wx.showLoading({

 

title: '蓝牙搜索中'

 

})

 

},

 

fail: function (err) {

 

if (err.isDiscovering) {

 

wx.showLoading({

 

title: '蓝牙搜索中'

 

})

 

} else {

 

that.startDiscoveryTimer = setTimeout(function () {

 

if (!that.connectedDevice.state) {

 

that.startBluetoothDevicesDiscovery();

 

}

 

}, 5000)

 

}

 

}

 

});

 

}

 

}

 

},

 

fail: function (err) {

 

console.log(err);

 

}

 

})

 

}

说明:

1、仅在安卓端设备上开启扫描附近蓝牙设备。

2、在开启成功的回调中开启发现新蓝牙设备的事件监听onBluetoothDeviceFound()。

 

onBluetoothDeviceFound代码:

 

[mw_shl_code=javascript,true]onBluetoothDeviceFound: function () {

 

var that = this;

 

wx.onBluetoothDeviceFound(function (res) {

 

console.log('new device list has founded');

 

if (res.devices[0]) {

 

var name = res.devices[0]['name'];

 

if (name.indexOf('FeiZhi') != -1) {

 

var deviceId = res.devices[0]['deviceId'];

 

console.log(deviceId);

 

that.deviceId = deviceId;

 

if (!that.isConnecting) {

 

that.startConnectDevices();

 

}

 

}

 

}

 

})

 

}