微信小程序蓝牙连接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代码:

  1. init: function (n) {
  2. this.list = [];
  3. this.serviceId = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E";
  4. this.serviceId_2 = "00001803-0000-1000-8000-00805F9B34FB";
  5. this.serviceId_3 = "00001814-0000-1000-8000-00805F9B34FB";
  6. this.serviceId_4 = "00001802-0000-1000-8000-00805F9B34FB";
  7. this.serviceId_5 = "00001804-0000-1000-8000-00805F9B34FB";
  8. this.serviceId_6 = "00001535-1212-EFDE-1523-785FEABCD123";
  9. this.characterId_write = "6E400042-B5A3-F393-E0A9-E50E24DCCA9E";
  10. this.characterId_read = "6E400012-B5A3-F393-E0A9-E50E24DCCA9E";
  11. this.connectDeviceIndex = 0;
  12. this.isGettingConnected = false;
  13. this.isDiscovering = false;
  14. this.isConnecting = false;
  15. this.connectedDevice = {};
  16. console.log('init state', this.connectedDevice.state);
  17. if (!this.connectedDevice.state || n == 200) {
  18. this.connectedDevice.state = false;
  19. this.connectedDevice.deviceId = '';
  20. this.adapterHasInit = false
  21. }
  22. this.startConnect();
  23. }

说明:

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

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

startConnect代码:

  1. startConnect: function () {
  2. var that = this;
  3. if (that.connectedDevice.state) return;
  4. that.connectedDevice.deviceId = "";
  5. that.connectedDevice.state = false;
  6. // 如果适配器已经初始化不在调用初始化(重复初始化会报错)
  7. if (this.adapterHasInit == undefined || this.adapterHasInit) return;
  8. wx.showLoading({
  9. title: '初始化蓝牙',
  10. duration: 2000
  11. });
  12. // 开启蓝牙适配器状态监听
  13. this.listenAdapterStateChange();
  14. // 初始化蓝牙适配器状态(必须步骤,否则无法进行后续的任何操作)
  15. wx.openBluetoothAdapter({
  16. success: function (res) {
  17. console.log("初始化蓝牙适配器成功");
  18. that.getBluetoothAdapterState();
  19. that.adapterHasInit = true;
  20. },
  21. fail: function (err) {
  22. console.log(err);
  23. wx.showLoading({
  24. title: '请开蓝牙',
  25. icon: 'loading',
  26. duration: 2000
  27. })
  28. }
  29. });
  30. }

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

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

getBluetoothAdapterState代码:

  1. getBluetoothAdapterState: function () {
  2. var that = this;
  3. wx.getBluetoothAdapterState({
  4. success: function (res) {
  5. console.log(res);
  6. var available = res.available;
  7. that.isDiscovering = res.discovering;
  8. if (!available) {
  9. wx.showLoading({
  10. title: '请开蓝牙',
  11. icon: 'loading',
  12. duration: 2000
  13. })
  14. } else {
  15. if (!that.connectedDevice['state']) {
  16. that.judegIfDiscovering(res.discovering);
  17. }
  18. }
  19. },
  20. fail: function (err) {
  21. console.log(err);
  22. }
  23. })
  24. }

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

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

judegIfDiscovering代码:

  1. judegIfDiscovering: function (discovering) {
  2. var that = this;
  3. if (this.isConnectinng) return;
  4. wx.getConnectedBluetoothDevices({
  5. services: [that.serviceId],
  6. success: function (res) {
  7. console.log("获取处于连接状态的设备", res);
  8. var devices = res['devices'];
  9. if (devices[0]) {
  10. if (that.isAndroidPlatform) {
  11. wx.showToast({
  12. title: '蓝牙连接成功',
  13. icon: 'success',
  14. duration: 2000
  15. });
  16. } else {
  17. that.getConnectedBluetoothDevices(256);
  18. }
  19. } else {
  20. if (discovering) {
  21. wx.showLoading({
  22. title: '蓝牙搜索中'
  23. })
  24. } else {
  25. if (that.isAndroidPlatform) {
  26. that.startBluetoothDevicesDiscovery();
  27. } else {
  28. that.getConnectedBluetoothDevices(267);
  29. }
  30. }
  31. }
  32. },
  33. fail: function (err) {
  34. console.log('getConnectedBluetoothDevices err 264', err);
  35. if (that.isAndroidPlatform) {
  36. that.startBluetoothDevicesDiscovery();
  37. } else {
  38. that.getConnectedBluetoothDevices(277);
  39. }
  40. }
  41. });
  42. }

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

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

startBluetoothDevicesDiscovery代码:

  1. startBluetoothDevicesDiscovery: function () {
  2. var that = this;
  3. if (!this.isAndroidPlatform) return;
  4. if (!this.connectedDevice['state']) {
  5. wx.getBluetoothAdapterState({
  6. success: function (res) {
  7. console.log(res);
  8. var available = res.available;
  9. that.isDiscovering = res.discovering;
  10. if (!available) {
  11. wx.showLoading({
  12. title: '请开蓝牙',
  13. icon: 'loading',
  14. duration: 2000
  15. })
  16. } else {
  17. if (res.discovering) {
  18. wx.showLoading({
  19. title: '蓝牙搜索中'
  20. })
  21. } else {
  22. wx.startBluetoothDevicesDiscovery({
  23. services: [],
  24. allowDuplicatesKey: true,
  25. success: function (res) {
  26. that.onBluetoothDeviceFound();
  27. wx.showLoading({
  28. title: '蓝牙搜索中'
  29. })
  30. },
  31. fail: function (err) {
  32. if (err.isDiscovering) {
  33. wx.showLoading({
  34. title: '蓝牙搜索中'
  35. })
  36. } else {
  37. that.startDiscoveryTimer = setTimeout(function () {
  38. if (!that.connectedDevice.state) {
  39. that.startBluetoothDevicesDiscovery();
  40. }
  41. }, 5000)
  42. }
  43. }
  44. });
  45. }
  46. }
  47. },
  48. fail: function (err) {
  49. console.log(err);
  50. }
  51. })
  52. }

说明:

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

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

onBluetoothDeviceFound代码:

  1. [mw_shl_code=javascript,true]onBluetoothDeviceFound: function () {
  2. var that = this;
  3. wx.onBluetoothDeviceFound(function (res) {
  4. console.log('new device list has founded');
  5. if (res.devices[0]) {
  6. var name = res.devices[0]['name'];
  7. if (name.indexOf('FeiZhi') != -1) {
  8. var deviceId = res.devices[0]['deviceId'];
  9. console.log(deviceId);
  10. that.deviceId = deviceId;
  11. if (!that.isConnecting) {
  12. that.startConnectDevices();
  13. }
  14. }
  15. }
  16. })
  17. }

说明: 
1、此处对已发现的蓝牙设备根据name属性进行了过滤。 
2、当筛选出含有需要连接的设备的name属性的设备是获取到deviceId,开始连接调用startConnectDevices()方法。

startConnectDevices代码:

  1. startConnectDevices: function (ltype, array) {
  2. var that = this;
  3. clearTimeout(this.getConnectedTimer);
  4. clearTimeout(this.startDiscoveryTimer);
  5. this.getConnectedTimer = null;
  6. this.startDiscoveryTimer = null;
  7. this.isConnectinng = true;
  8. wx.showLoading({
  9. title: '正在连接'
  10. });
  11. that.stopBluetoothDevicesDiscovery();
  12. wx.createBLEConnection({
  13. deviceId: that.deviceId,
  14. success: function (res) {
  15. console.log('连接成功', res);
  16. wx.showLoading({
  17. title: '正在连接'
  18. });
  19. that.connectedDevice.state = true;
  20. that.connectedDevice.deviceId = that.deviceId;
  21. if (res.errCode == 0) {
  22. setTimeout(function () {
  23. that.getService(that.deviceId);
  24. }, 5000)
  25. }
  26. wx.onBLEConnectionStateChange(function (res) {
  27. console.log('连接变化', res);
  28. that.connectedDevice.state = res.connected;
  29. that.connectedDevice.deviceId = res.deviceId;
  30. if (!res.connected) {
  31. that.init('200');
  32. }
  33. });
  34. },
  35. fail: function (err) {
  36. console.log('连接失败:', err);
  37. wx.hideLoading();
  38. if (ltype == 'loop') {
  39. array = array.splice(0, 1);
  40. console.log(array);
  41. that.loopConnect(array);
  42. } else {
  43. if (that.isAndroidPlatform) {
  44. that.startBluetoothDevicesDiscovery();
  45. } else {
  46. that.getConnectedBluetoothDevices(488);
  47. }
  48. }
  49. },
  50. complete: function () {
  51. that.isConnectinng = false;
  52. }
  53. });
  54. }

说明: 
1、开启连接后终止扫描(获取已配对)方法。 
2、根据deviceId创建低功耗蓝牙连接。如果连接成功,就继续做后续读写操作。 
3、如果连接失败根据设备系统分别调用startBluetoothDevicesDiscovery() 或 getConnectedBluetoothDevices();

getConnectedBluetoothDevices代码:

  1. getConnectedBluetoothDevices: function (n) {
  2. var that = this;
  3. that.isGettingConnected = true;
  4. wx.showLoading({
  5. title: '蓝牙搜索中'
  6. });
  7. wx.getConnectedBluetoothDevices({
  8. services: [that.serviceId],
  9. success: function (res) {
  10. console.log("获取处于连接状态的设备", res);
  11. var devices = res['devices'],
  12. flag = false,
  13. index = 0,
  14. conDevList = [];
  15. devices.forEach(function (value, index, array) {
  16. if (value['name'].indexOf('FeiZhi') != -1) {
  17. // 如果存在包含FeiZhi字段的设备
  18. flag = true;
  19. index += 1;
  20. conDevList.push(value['deviceId']);
  21. that.deviceId = value['deviceId'];
  22. }
  23. });
  24. if (flag) {
  25. that.connectDeviceIndex = 0;
  26. that.loopConnect(conDevList);
  27. } else {
  28. that.failToGetConnected();
  29. }
  30. },
  31. fail: function (err) {
  32. that.failToGetConnected();
  33. },
  34. complete: function () {
  35. that.isGettingConnected = false;
  36. }
  37. });
  38. }

说明:如果获取蓝牙已配对的蓝牙设备失败了,或获取到的列表为空调用failToGetConnected();

failToGetConnected代码:

  1. failToGetConnected: function () {
  2. var that = this;
  3. if (!that.getConnectedTimer) {
  4. clearTimeout(that.getConnectedTimer);
  5. that.getConnectedTimer = null;
  6. }
  7. that.getConnectedTimer = setTimeout(function () {
  8. wx.getBluetoothAdapterState({
  9. success: function (res) {
  10. console.log(res);
  11. var available = res.available;
  12. if (!available) {
  13. wx.showLoading({
  14. title: '请开蓝牙',
  15. icon: 'loading',
  16. duration: 2000
  17. })
  18. } else {
  19. if (!that.connectedDevice['state']) {
  20. that.getConnectedBluetoothDevices();
  21. }
  22. }
  23. },
  24. fail: function (err) {
  25. console.log(err);
  26. }
  27. })
  28. }, 5000);
  29. }

说明: 
1、该方法调用成功后返回的devices是一个数组包含多个已经系统配对的蓝牙设备。 
2、如果devices列表获取到调用loopConnect()方法开始递归调用连接蓝牙设备。

loopConnect代码:

  1. loopConnect: function (array) {
  2. var that = this;
  3. var listLen = array.length;
  4. if (array[0]) {
  5. that.deviceId = array[0];
  6. if (!that.isConnecting) {
  7. that.startConnectDevices('loop', array);
  8. }
  9. } else {
  10. console.log('已配对的设备小程序蓝牙连接失败');
  11. if (!that.isAndroidPlatform) {
  12. that.getConnectedBluetoothDevices(431);
  13. }
  14. }
  15. }

说明:looConnect在创建连接的方法连接失败后会操作删除数组的第一个值,然后继续调用该方法,直到其中所有的设备都连接过。 
差点漏了:在app.js的onShow里调用init()方法。

特别说明:

1、安卓和IOS的蓝牙连接在当前版本中推荐采用不同方式。安卓设备直接使用小程序的蓝牙连接,取消系统配对。IOS设备先系统配对在打开小程序可以时效秒连接成功。 
2、此版本的连接仍然有待完善,连接不会自动终止(需要的可以自己加),会无限扫描重连,直到成功。 
3、链接成功后的操作如果写入数据和开启notify需要同时进行,建议先写入,后开启notify。(原因未知,否则必然出现10008错误)。


本文转载:CSDN博客