网友真实露脸自拍10p,成人国产精品秘?久久久按摩,国产精品久久久久久无码不卡,成人免费区一区二区三区

小程序模板網(wǎng)

熊晨灃藍(lán)牙實戰(zhàn)--小程序藍(lán)牙連接2.0

發(fā)布時間:2018-01-02 09:01 所屬欄目:小程序開發(fā)教程

微信小程序藍(lán)牙連接2.0說明:

1、本版本區(qū)分了ANDROID和IOS系統(tǒng)下藍(lán)牙連接的不同方式。 
2、兼容了更多情況下的鏈接包括:

(1)未開啟設(shè)備藍(lán)牙,當(dāng)監(jiān)聽到開啟了藍(lán)牙后自動開始連接。 
(2)初始化藍(lán)牙失敗后每3000ms自動重新初始化藍(lán)牙適配器。 
(3)安卓端開啟藍(lán)牙適配器掃描失敗,每3000ms自動重新開啟。 
(4)IOS端獲取已連接藍(lán)牙設(shè)備為空,每3000ms自動重新獲取。 
(5)安卓端藍(lán)牙開始鏈接后中斷掃描,連接失敗了,重新開始掃描。 
(6)IOS端開始連接設(shè)備后,停止獲取已連接設(shè)備,連接失敗自動重新開啟獲取。 
(7)連接成功后,關(guān)閉系統(tǒng)藍(lán)牙,藍(lán)牙適配器重置。 
(8)連接成功后,關(guān)閉系統(tǒng)藍(lán)牙,再次打開藍(lán)牙,自動重新開始連接。 
(9)連接成功后,關(guān)閉目標(biāo)藍(lán)牙設(shè)備,自動重新開始掃描(獲取)。 
(10)連接成功后,最小化小程序(連接未中斷),打開小程序顯示已連接。 
(11)連接成功后,殺掉小程序進(jìn)程,連接關(guān)閉,自動重新開始掃描(獲取)。

3、想起來了再來更新....。 
4、流程圖,明天或后天或...誰有空幫我畫一下也行。

我的連接是在App.js中做的。 
在App.js中的onLaunch觸發(fā)是調(diào)用 init()方法。 
init代碼:

 

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

說明:

1、 serviceId_2~6 是我已知的想要連接的藍(lán)牙設(shè)備的serviceId可以只寫一個。  2、characterId_write 是我已知的想要連接的藍(lán)牙設(shè)備寫入數(shù)據(jù)的特征值。  3、characterId_read是我已知的想要連接的藍(lán)牙設(shè)備讀取數(shù)據(jù)的特征值。  (以上3個都是為了做比對,真實的操作按照獲取到的sericeid, characterid為準(zhǔn))。  4、connectedDevice 是已連接了的設(shè)備信息對象。

init完成后開始調(diào)用連接 startConnect();

 

startConnect代碼:

 

  1. startConnect: function () {
  2.  
  3. var that = this;
  4.  
  5. if (that.connectedDevice.state) return;
  6.  
  7. that.connectedDevice.deviceId = "";
  8.  
  9. that.connectedDevice.state = false;
  10.  
  11. // 如果適配器已經(jīng)初始化不在調(diào)用初始化(重復(fù)初始化會報錯)
  12.  
  13. if (this.adapterHasInit == undefined || this.adapterHasInit) return;
  14.  
  15. wx.showLoading({
  16.  
  17. title: '初始化藍(lán)牙',
  18.  
  19. duration: 2000
  20.  
  21. });
  22.  
  23. // 開啟藍(lán)牙適配器狀態(tài)監(jiān)聽
  24.  
  25. this.listenAdapterStateChange();
  26.  
  27. // 初始化藍(lán)牙適配器狀態(tài)(必須步驟,否則無法進(jìn)行后續(xù)的任何操作)
  28.  
  29. wx.openBluetoothAdapter({
  30.  
  31. success: function (res) {
  32.  
  33. console.log("初始化藍(lán)牙適配器成功");
  34.  
  35. that.getBluetoothAdapterState();
  36.  
  37. that.adapterHasInit = true;
  38.  
  39. },
  40.  
  41. fail: function (err) {
  42.  
  43. console.log(err);
  44.  
  45. wx.showLoading({
  46.  
  47. title: '請開藍(lán)牙',
  48.  
  49. icon: 'loading',
  50.  
  51. duration: 2000
  52.  
  53. })
  54.  
  55. }
  56.  
  57. });
  58.  
  59. }

說明:這段有注釋,就不多說了,比較簡單。

在初始化藍(lán)牙適配器狀態(tài)成功后調(diào)用getBluetoothAdapterState()方法。

 

getBluetoothAdapterState代碼:

 

  1. getBluetoothAdapterState: function () {
  2.  
  3. var that = this;
  4.  
  5. wx.getBluetoothAdapterState({
  6.  
  7. success: function (res) {
  8.  
  9. console.log(res);
  10.  
  11. var available = res.available;
  12.  
  13. that.isDiscovering = res.discovering;
  14.  
  15. if (!available) {
  16.  
  17. wx.showLoading({
  18.  
  19. title: '請開藍(lán)牙',
  20.  
  21. icon: 'loading',
  22.  
  23. duration: 2000
  24.  
  25. })
  26.  
  27. } else {
  28.  
  29. if (!that.connectedDevice['state']) {
  30.  
  31. that.judegIfDiscovering(res.discovering);
  32.  
  33. }
  34.  
  35. }
  36.  
  37. },
  38.  
  39. fail: function (err) {
  40.  
  41. console.log(err);
  42.  
  43. }
  44.  
  45. })
  46.  
  47. }

說明:此方法是用來獲取當(dāng)前藍(lán)牙狀態(tài)。

當(dāng)檢測到藍(lán)牙可用時調(diào)用judegIfDiscovering方法。

 

judegIfDiscovering代碼:

 

  1. judegIfDiscovering: function (discovering) {
  2.  
  3. var that = this;
  4.  
  5. if (this.isConnectinng) return;
  6.  
  7. wx.getConnectedBluetoothDevices({
  8.  
  9. services: [that.serviceId],
  10.  
  11. success: function (res) {
  12.  
  13. console.log("獲取處于連接狀態(tài)的設(shè)備", res);
  14.  
  15. var devices = res['devices'];
  16.  
  17. if (devices[0]) {
  18.  
  19. if (that.isAndroidPlatform) {
  20.  
  21. wx.showToast({
  22.  
  23. title: '藍(lán)牙連接成功',
  24.  
  25. icon: 'success',
  26.  
  27. duration: 2000
  28.  
  29. });
  30.  
  31. } else {
  32.  
  33. that.getConnectedBluetoothDevices(256);
  34.  
  35. }
  36.  
  37. } else {
  38.  
  39. if (discovering) {
  40.  
  41. wx.showLoading({
  42.  
  43. title: '藍(lán)牙搜索中'
  44.  
  45. })
  46.  
  47. } else {
  48.  
  49. if (that.isAndroidPlatform) {
  50.  
  51. that.startBluetoothDevicesDiscovery();
  52.  
  53. } else {
  54.  
  55. that.getConnectedBluetoothDevices(267);
  56.  
  57. }
  58.  
  59. }
  60.  
  61. }
  62.  
  63. },
  64.  
  65. fail: function (err) {
  66.  
  67. console.log('getConnectedBluetoothDevices err 264', err);
  68.  
  69. if (that.isAndroidPlatform) {
  70.  
  71. that.startBluetoothDevicesDiscovery();
  72.  
  73. } else {
  74.  
  75. that.getConnectedBluetoothDevices(277);
  76.  
  77. }
  78.  
  79. }
  80.  
  81. });
  82.  
  83. }

說明:  1、此方法是用來判斷是否正在掃描。  2、isAndroidPlatform 是通過小程序的getSystemInfo獲取到的判斷是安卓設(shè)備還是IOS設(shè)備。

如果是安卓設(shè)備調(diào)用startBluetoothDevicesDiscovery()開啟掃描,如果是IOS設(shè)備調(diào)用getConnectedBluetoothDevices() 開啟獲取已配對的藍(lán)牙設(shè)備。

 

startBluetoothDevicesDiscovery代碼:

 

  1. startBluetoothDevicesDiscovery: function () {
  2.  
  3. var that = this;
  4.  
  5. if (!this.isAndroidPlatform) return;
  6.  
  7. if (!this.connectedDevice['state']) {
  8.  
  9. wx.getBluetoothAdapterState({
  10.  
  11. success: function (res) {
  12.  
  13. console.log(res);
  14.  
  15. var available = res.available;
  16.  
  17. that.isDiscovering = res.discovering;
  18.  
  19. if (!available) {
  20.  
  21. wx.showLoading({
  22.  
  23. title: '請開藍(lán)牙',
  24.  
  25. icon: 'loading',
  26.  
  27. duration: 2000
  28.  
  29. })
  30.  
  31. } else {
  32.  
  33. if (res.discovering) {
  34.  
  35. wx.showLoading({
  36.  
  37. title: '藍(lán)牙搜索中'
  38.  
  39. })
  40.  
  41. } else {
  42.  
  43. wx.startBluetoothDevicesDiscovery({
  44.  
  45. services: [],
  46.  
  47. allowDuplicatesKey: true,
  48.  
  49. success: function (res) {
  50.  
  51. that.onBluetoothDeviceFound();
  52.  
  53. wx.showLoading({
  54.  
  55. title: '藍(lán)牙搜索中'
  56.  
  57. })
  58.  
  59. },
  60.  
  61. fail: function (err) {
  62.  
  63. if (err.isDiscovering) {
  64.  
  65. wx.showLoading({
  66.  
  67. title: '藍(lán)牙搜索中'
  68.  
  69. })
  70.  
  71. } else {
  72.  
  73. that.startDiscoveryTimer = setTimeout(function () {
  74.  
  75. if (!that.connectedDevice.state) {
  76.  
  77. that.startBluetoothDevicesDiscovery();
  78.  
  79. }
  80.  
  81. }, 5000)
  82.  
  83. }
  84.  
  85. }
  86.  
  87. });
  88.  
  89. }
  90.  
  91. }
  92.  
  93. },
  94.  
  95. fail: function (err) {
  96.  
  97. console.log(err);
  98.  
  99. }
  100.  
  101. })
  102.  
  103. }

說明:

1、僅在安卓端設(shè)備上開啟掃描附近藍(lán)牙設(shè)備。

2、在開啟成功的回調(diào)中開啟發(fā)現(xiàn)新藍(lán)牙設(shè)備的事件監(jiān)聽onBluetoothDeviceFound()。

 

onBluetoothDeviceFound代碼:

 

  1. [mw_shl_code=javascript,true]onBluetoothDeviceFound: function () {
  2.  
  3. var that = this;
  4.  
  5. wx.onBluetoothDeviceFound(function (res) {
  6.  
  7. console.log('new device list has founded');
  8.  
  9. if (res.devices[0]) {
  10.  
  11. var name = res.devices[0]['name'];
  12.  
  13. if (name.indexOf('FeiZhi') != -1) {
  14.  
  15. var deviceId = res.devices[0]['deviceId'];
  16.  
  17. console.log(deviceId);
  18.  
  19. that.deviceId = deviceId;
  20.  
  21. if (!that.isConnecting) {
  22.  
  23. that.startConnectDevices();
  24.  
  25. }
  26.  
  27. }
  28.  
  29. }
  30.  
  31. })
  32.  
  33. }

說明:  1、此處對已發(fā)現(xiàn)的藍(lán)牙設(shè)備根據(jù)name屬性進(jìn)行了過濾。  2、當(dāng)篩選出含有需要連接的設(shè)備的name屬性的設(shè)備是獲取到deviceId,開始連接調(diào)用startConnectDevices()方法。

 

startConnectDevices代碼:

 

  1. startConnectDevices: function (ltype, array) {
  2.  
  3. var that = this;
  4.  
  5. clearTimeout(this.getConnectedTimer);
  6.  
  7. clearTimeout(this.startDiscoveryTimer);
  8.  
  9. this.getConnectedTimer = null;
  10.  
  11. this.startDiscoveryTimer = null;
  12.  
  13. this.isConnectinng = true;
  14.  
  15. wx.showLoading({
  16.  
  17. title: '正在連接'
  18.  
  19. });
  20.  
  21. that.stopBluetoothDevicesDiscovery();
  22.  
  23. wx.createBLEConnection({
  24.  
  25. deviceId: that.deviceId,
  26.  
  27. success: function (res) {
  28.  
  29. console.log('連接成功', res);
  30.  
  31. wx.showLoading({
  32.  
  33. title: '正在連接'
  34.  
  35. });
  36.  
  37. that.connectedDevice.state = true;
  38.  
  39. that.connectedDevice.deviceId = that.deviceId;
  40.  
  41. if (res.errCode == 0) {
  42.  
  43. setTimeout(function () {
  44.  
  45. that.getService(that.deviceId);
  46.  
  47. }, 5000)
  48.  
  49. }
  50.  
  51. wx.onBLEConnectionStateChange(function (res) {
  52.  
  53. console.log('連接變化', res);
  54.  
  55. that.connectedDevice.state = res.connected;
  56.  
  57. that.connectedDevice.deviceId = res.deviceId;
  58.  
  59. if (!res.connected) {
  60.  
  61. that.init('200');
  62.  
  63. }
  64.  
  65. });
  66.  
  67. },
  68.  
  69. fail: function (err) {
  70.  
  71. console.log('連接失敗:', err);
  72.  
  73. wx.hideLoading();
  74.  
  75. if (ltype == 'loop') {
  76.  
  77. array = array.splice(0, 1);
  78.  
  79. console.log(array);
  80.  
  81. that.loopConnect(array);
  82.  
  83. } else {
  84.  
  85. if (that.isAndroidPlatform) {
  86.  
  87. that.startBluetoothDevicesDiscovery();
  88.  
  89. } else {
  90.  
  91. that.getConnectedBluetoothDevices(488);
  92.  
  93. }
  94.  
  95. }
  96.  
  97. },
  98.  
  99. complete: function () {
  100.  
  101. that.isConnectinng = false;
  102.  
  103. }
  104.  
  105. });
  106.  
  107. }

說明:  1、開啟連接后終止掃描(獲取已配對)方法。  2、根據(jù)deviceId創(chuàng)建低功耗藍(lán)牙連接。如果連接成功,就繼續(xù)做后續(xù)讀寫操作。  3、如果連接失敗根據(jù)設(shè)備系統(tǒng)分別調(diào)用startBluetoothDevicesDiscovery() 或 getConnectedBluetoothDevices();

 

getConnectedBluetoothDevices代碼:

 

  1. getConnectedBluetoothDevices: function (n) {
  2.  
  3. var that = this;
  4.  
  5. that.isGettingConnected = true;
  6.  
  7. wx.showLoading({
  8.  
  9. title: '藍(lán)牙搜索中'
  10.  
  11. });
  12.  
  13. wx.getConnectedBluetoothDevices({
  14.  
  15. services: [that.serviceId],
  16.  
  17. success: function (res) {
  18.  
  19. console.log("獲取處于連接狀態(tài)的設(shè)備", res);
  20.  
  21. var devices = res['devices'],
  22.  
  23. flag = false,
  24.  
  25. index = 0,
  26.  
  27. conDevList = [];
  28.  
  29. devices.forEach(function (value, index, array) {
  30.  
  31. if (value['name'].indexOf('FeiZhi') != -1) {
  32.  
  33. // 如果存在包含F(xiàn)eiZhi字段的設(shè)備
  34.  
  35. flag = true;
  36.  
  37. index += 1;
  38.  
  39. conDevList.push(value['deviceId']);
  40.  
  41. that.deviceId = value['deviceId'];
  42.  
  43. }
  44.  
  45. });
  46.  
  47. if (flag) {
  48.  
  49. that.connectDeviceIndex = 0;
  50.  
  51. that.loopConnect(conDevList);
  52.  
  53. } else {
  54.  
  55. that.failToGetConnected();
  56.  
  57. }
  58.  
  59. },
  60.  
  61. fail: function (err) {
  62.  
  63. that.failToGetConnected();
  64.  
  65. },
  66.  
  67. complete: function () {
  68.  
  69. that.isGettingConnected = false;
  70.  
  71. }
  72.  
  73. });
  74.  
  75. }

說明:如果獲取藍(lán)牙已配對的藍(lán)牙設(shè)備失敗了,或獲取到的列表為空調(diào)用failToGetConnected();

 

failToGetConnected代碼:

 

  1. failToGetConnected: function () {
  2.  
  3. var that = this;
  4.  
  5. if (!that.getConnectedTimer) {
  6.  
  7. clearTimeout(that.getConnectedTimer);
  8.  
  9. that.getConnectedTimer = null;
  10.  
  11. }
  12.  
  13. that.getConnectedTimer = setTimeout(function () {
  14.  
  15. wx.getBluetoothAdapterState({
  16.  
  17. success: function (res) {
  18.  
  19. console.log(res);
  20.  
  21. var available = res.available;
  22.  
  23. if (!available) {
  24.  
  25. wx.showLoading({
  26.  
  27. title: '請開藍(lán)牙',
  28.  
  29. icon: 'loading',
  30.  
  31. duration: 2000
  32.  
  33. })
  34.  
  35. } else {
  36.  
  37. if (!that.connectedDevice['state']) {
  38.  
  39. that.getConnectedBluetoothDevices();
  40.  
  41. }
  42.  
  43. }
  44.  
  45. },
  46.  
  47. fail: function (err) {
  48.  
  49. console.log(err);
  50.  
  51. }
  52.  
  53. })
  54.  
  55. }, 5000);
  56.  
  57. }

說明:  1、該方法調(diào)用成功后返回的devices是一個數(shù)組包含多個已經(jīng)系統(tǒng)配對的藍(lán)牙設(shè)備。  2、如果devices列表獲取到調(diào)用loopConnect()方法開始遞歸調(diào)用連接藍(lán)牙設(shè)備。

 

loopConnect代碼:

 

  1. loopConnect: function (array) {
  2.  
  3. var that = this;
  4.  
  5. var listLen = array.length;
  6.  
  7. if (array[0]) {
  8.  
  9. that.deviceId = array[0];
  10.  
  11. if (!that.isConnecting) {
  12.  
  13. that.startConnectDevices('loop', array);
  14.  
  15. }
  16.  
  17. } else {
  18.  
  19. console.log('已配對的設(shè)備小程序藍(lán)牙連接失敗');
  20.  
  21. if (!that.isAndroidPlatform) {
  22.  
  23. that.getConnectedBluetoothDevices(431);
  24.  
  25. }
  26.  
  27. }
  28.  
  29. }

說明:looConnect在創(chuàng)建連接的方法連接失敗后會操作刪除數(shù)組的第一個值,然后繼續(xù)調(diào)用該方法,直到其中所有的設(shè)備都連接過。  差點漏了:在app.js的onShow里調(diào)用init()方法。

 

特別說明:

1、安卓和IOS的藍(lán)牙連接在當(dāng)前版本中推薦采用不同方式。安卓設(shè)備直接使用小程序的藍(lán)牙連接,取消系統(tǒng)配對。IOS設(shè)備先系統(tǒng)配對在打開小程序可以時效秒連接成功。  2、此版本的連接仍然有待完善,連接不會自動終止(需要的可以自己加),會無限掃描重連,直到成功。  3、鏈接成功后的操作如果寫入數(shù)據(jù)和開啟notify需要同時進(jìn)行,建議先寫入,后開啟notify。(原因未知,否則必然出現(xiàn)10008錯誤)。


易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開源 碼云倉庫:starfork
本文地址:http://www.xiuhaier.com/wxmini/doc/course/18340.html 復(fù)制鏈接 如需定制請聯(lián)系易優(yōu)客服咨詢:800182392 點擊咨詢
QQ在線咨詢
AI智能客服 ×