北京 设计 网站建设,wordpress第三方收款,了解当前各类网站建设价格,昆明seo案例haas506 2.0开发教程-example-蓝牙多设备扫描案例说明蓝牙信息克隆1.手机蓝牙改名信息克隆代码测试案例说明
开发板扫描蓝牙设备#xff0c;获取并打印蓝牙设备mac地址。mac地址每个设备不同#xff0c;且不能更改。本案例仅适用于M320开发板和HD1-RTU。案例使用手机与iBeac…
haas506 2.0开发教程-example-蓝牙多设备扫描案例说明蓝牙信息克隆1.手机蓝牙改名信息克隆代码测试案例说明
开发板扫描蓝牙设备获取并打印蓝牙设备mac地址。mac地址每个设备不同且不能更改。本案例仅适用于M320开发板和HD1-RTU。案例使用手机与iBeacon作为从机。开发板根据设备名称进行连接要同时扫描到2个设备手机的蓝牙名称需要改成与iBeacon相同的名称。
蓝牙信息克隆
使用手机克隆iBeacon的名字信息。
1.手机蓝牙改名
手机下载nRF connect蓝牙调试工具。 iBeacon设备名称将手机蓝牙改成一样的名字
选择ADVERTISER后点击图标改名 信息克隆
点开iBeacon设备后点击CLONE 这样就得到了2个同名的设备。
代码
注意更改代码中的设备名称
main.py
# This example finds and connects to a BLE temperature sensor (e.g. the one in ble_temperature.py).# from ast import While
# from logging.config import valid_ident
# from multiprocessing.sharedctypes import Value
# from sys import platlibdir
# from this import d
# from traceback import print_list
import bluetooth
from bluetooth import BLE
from bluetooth import UUID
# import random
import ustruct#import time
import micropythonimport utime as time#from ble_advertising import decode_services, decode_namefrom micropython import const_IRQ_CENTRAL_CONNECT const(1)
_IRQ_CENTRAL_DISCONNECT const(2)
_IRQ_GATTS_WRITE const(3)
_IRQ_GATTS_READ_REQUEST const(4)
_IRQ_SCAN_RESULT const(5)
_IRQ_SCAN_DONE const(6)
_IRQ_PERIPHERAL_CONNECT const(7)
_IRQ_PERIPHERAL_DISCONNECT const(8)
_IRQ_GATTC_SERVICE_RESULT const(9)
_IRQ_GATTC_SERVICE_DONE const(10)
_IRQ_GATTC_CHARACTERISTIC_RESULT const(11)
_IRQ_GATTC_CHARACTERISTIC_DONE const(12)
_IRQ_GATTC_DESCRIPTOR_RESULT const(13)
_IRQ_GATTC_DESCRIPTOR_DONE const(14)
_IRQ_GATTC_READ_RESULT const(15)
_IRQ_GATTC_READ_DONE const(16)
_IRQ_GATTC_WRITE_DONE const(17)
_IRQ_GATTC_NOTIFY const(18)
_IRQ_GATTC_INDICATE const(19)_ADV_IND const(0x00)
_ADV_DIRECT_IND const(0x01)
_ADV_SCAN_IND const(0x02)
_ADV_NONCONN_IND const(0x03)# org.bluetooth.service.environmental_sensing
# _ENV_SENSE_UUID bluetooth.UUID(0x30AF)
_ENV_SENSE_UUID bluetooth.UUID(FDA50693-A4E2-4FB1-AFCF-C6EB07647825)
# org.bluetooth.characteristic.temperature
_TEMP_UUID bluetooth.UUID(0x2A6E)
_TEMP_CHAR (_TEMP_UUID,bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY,
)
_ENV_SENSE_SERVICE (_ENV_SENSE_UUID,(_TEMP_CHAR,),
)_UART_SERVICE_UUID bluetooth.UUID(6E400001-B5A3-F393-E0A9-E50E24DCCA77)
_UART_RX_CHAR_UUID bluetooth.UUID(6E400002-B5A3-F393-E0A9-E50E24DCCA77)
_UART_TX_CHAR_UUID bluetooth.UUID(6E400003-B5A3-F393-E0A9-E50E24DCCA77)_MY_SERVICE_UUID bluetooth.UUID(0x181A)# org.bluetooth.characteristic.gap.appearance.xml
_ADV_APPEARANCE_GENERIC_THERMOMETER const(768)_ADV_TYPE_FLAGS const(0x01)
_ADV_TYPE_NAME const(0x09)
_ADV_TYPE_UUID16_COMPLETE const(0x3)
_ADV_TYPE_UUID32_COMPLETE const(0x5)
_ADV_TYPE_UUID128_COMPLETE const(0x7)
_ADV_TYPE_UUID16_MORE const(0x2)
_ADV_TYPE_UUID32_MORE const(0x4)
_ADV_TYPE_UUID128_MORE const(0x6)
_ADV_TYPE_APPEARANCE const(0x19)def decode_field(payload, adv_type):i 0result []while i 1 len(payload):if payload[i 1] adv_type:result.append(payload[i 2 : i payload[i] 1])i 1 payload[i]return resultdef decode_name(payload):n decode_field(payload, _ADV_TYPE_NAME)# print(n:,n)return str(n[0], utf-8) if n else def decode_mac(addr):if isinstance(addr, memoryview):addr bytes(addr)assert isinstance(addr,bytes) and len(addr) 6,ValueError(mac address value error)return :.join([%02X % byte for byte in addr])def print_hex(bytes):l [hex(int(i)) for i in bytes]return .join(l)def decode_services(payload):services []for u in decode_field(payload, _ADV_TYPE_UUID16_COMPLETE):services.append(bluetooth.UUID(ustruct.unpack(h, u)[0]))for u in decode_field(payload, _ADV_TYPE_UUID32_COMPLETE):services.append(bluetooth.UUID(ustruct.unpack(d, u)[0]))for u in decode_field(payload, _ADV_TYPE_UUID128_COMPLETE):services.append(bluetooth.UUID(u))return servicesclass BLETemperatureCentral:def __init__(self, ble):self._ble bleself._ble.gap_scan_name(X622080084)self._ble.gattc_set_uuids(0xFFF2, 0xFFF1)self._ble.active(True)time.sleep_ms(3000)self._ble.irq(self._irq)self._reset()def _reset(self):# Cached name and address from a successful scan.self._name Noneself._addr_type Noneself._addr Noneself._mac Noneself._adv_data None# Cached value (if we have one)self._value None# Callbacks for completion of various operations.# These reset back to None after being invoked.self._scan_callback Noneself._conn_callback Noneself._read_callback None# Persistent callback for when new data is notified from the device.self._notify_callback None# Connected device.self._conn_handle Noneself._start_handle Noneself._end_handle Noneself._value_handle Nonedef _irq(self, event, data):if event _IRQ_SCAN_RESULT:# time.sleep_ms(1)addr_type, addr, adv_type, rssi, adv_data data# print(ble data:{}.format(adv_data))# if adv_type in (_ADV_IND, _ADV_DIRECT_IND) and _ENV_SENSE_UUID in decode_services(adv_data):if adv_type in (_ADV_IND, _ADV_DIRECT_IND):# if adv_type in (_ADV_DIRECT_IND): #ibeacon的类型# Found a potential device, remember it and stop scanning.self._addr_type addr_typeself._addr bytes(addr) # Note: addr buffer is owned by caller so need to copy it.self._name decode_name(adv_data)self._mac decode_mac(addr)# self._adv_data bytes(# adv_data# ) # Note: addr buffer is owned by caller so need to copy it.# self._name decode_name(adv_data) or ?# print(ble addr:,self._mac )# print(ble data:,data )elif event _IRQ_SCAN_DONE:#time.sleep_ms(1000)self._ble.gap_scan(None)#print(##### scan done #####)val 1if self._scan_callback:if self._addr:# Found a device during the scan (and the scan was explicitly stopped).self._scan_callback(self._addr_type, self._addr, self._name)self._scan_callback Noneelse:# Scan timed out.self._scan_callback(None, None, None)elif event _IRQ_PERIPHERAL_CONNECT:print(connect)elif event _IRQ_PERIPHERAL_DISCONNECT:print(disconnect)elif event _IRQ_GATTC_SERVICE_RESULT:# Connected device returned a service.conn_handle, start_handle, end_handle, uuid dataif conn_handle self._conn_handle and uuid _ENV_SENSE_UUID:self._start_handle, self._end_handle start_handle, end_handleelif event _IRQ_GATTC_SERVICE_DONE:# Service query complete.if self._start_handle and self._end_handle:self._ble.gattc_discover_characteristics(self._conn_handle, self._start_handle, self._end_handle)else:print(Failed to find environmental sensing service.)elif event _IRQ_GATTC_CHARACTERISTIC_RESULT:# Connected device returned a characteristic.conn_handle, def_handle, value_handle, properties, uuid dataif conn_handle self._conn_handle and uuid _TEMP_UUID:self._value_handle value_handleelif event _IRQ_GATTC_CHARACTERISTIC_DONE:# Characteristic query complete.if self._value_handle:# Weve finished connecting and discovering device, fire the connect callback.if self._conn_callback:self._conn_callback()else:print(Failed to find temperature characteristic.)elif event _IRQ_GATTC_READ_RESULT:buffer self._ble.gattc_read(0,0)print(recv data:,buffer)elif event _IRQ_GATTC_READ_DONE:# Read completed (no-op).conn_handle, value_handle, status dataelif event _IRQ_GATTC_NOTIFY:# The ble_temperature.py demo periodically notifies its value.conn_handle, value_handle, notify_data dataif conn_handle self._conn_handle and value_handle self._value_handle:self._update_value(notify_data)if self._notify_callback:self._notify_callback(self._value)elif event _IRQ_GATTC_WRITE_DONE:conn_handle, value_handle, status dataprint(TX complete)# Returns true if weve successfully connected and discovered characteristics.def is_connected(self):return self._conn_handle is not None and self._value_handle is not None# Find a device advertising the environmental sensor service.def scan(self, callbackNone):self._addr_type Noneself._addr Noneself._scan_callback callbackself._ble.gap_scan(4000, 6000, 6000)# Connect to the specified device (otherwise use cached address from a scan).def connect(self, addr_typeNone, addrNone, callbackNone):self._addr_type addr_type or self._addr_typeself._addr addr or self._addrself._conn_callback callbackif self._addr_type is None or self._addr is None:return Falseself._ble.gap_connect(self._addr_type, self._addr)return True# Disconnect from current device.def disconnect(self):if not self._conn_handle:returnself._ble.gap_disconnect(self._conn_handle)self._reset()# Issues an (asynchronous) read, will invoke callback with data.def read(self, callback):if not self.is_connected():returnself._read_callback callbackself._ble.gattc_read(self._conn_handle, self._value_handle)# Sets a callback to be invoked when the device notifies us.def on_notify(self, callback):self._notify_callback callbackdef _update_value(self, data):# Data is sint16 in degrees Celsius with a resolution of 0.01 degrees Celsius.self._value ustruct.unpack(h, data)[0] / 100return self._valuedef value(self):return self._valuedef write(self, v, responseFalse):# if not self.is_connected():# returnself._ble.gattc_write(0, 0, v, 1 if response else 0)print(data_send:, v)global v
def demo():time.sleep_ms(2000)ble bluetooth.BLE()central BLETemperatureCentral(ble)time.sleep_ms(2000)not_found Falsedef on_scan(addr_type, addr, name):if addr_type is not None:print(Found sensor addr_type:, addr_type)print(Found sensor addr:, print_hex(addr))print(Found sensor name:, name)global vv 1else:nonlocal not_foundnot_found Trueprint(No sensor found.)while True :central.scan(callbackon_scan)time.sleep_ms(2000)# if v 1:# central.connect()# while True:# ble.gattc_notify(1, 1)# # central.write(123, False)# time.sleep_ms(1000)if __name__ __main__:demo()
测试
Haas506可以搜索到2个不同mac地址的设备并将mac地址打印。 文章转载自: http://www.morning.qbksx.cn.gov.cn.qbksx.cn http://www.morning.dshxj.cn.gov.cn.dshxj.cn http://www.morning.ytrbq.cn.gov.cn.ytrbq.cn http://www.morning.lyldhg.cn.gov.cn.lyldhg.cn http://www.morning.hwbmn.cn.gov.cn.hwbmn.cn http://www.morning.htbsk.cn.gov.cn.htbsk.cn http://www.morning.stmkm.cn.gov.cn.stmkm.cn http://www.morning.zfqdt.cn.gov.cn.zfqdt.cn http://www.morning.ldqzz.cn.gov.cn.ldqzz.cn http://www.morning.xcszl.cn.gov.cn.xcszl.cn http://www.morning.rbhcx.cn.gov.cn.rbhcx.cn http://www.morning.bqdpy.cn.gov.cn.bqdpy.cn http://www.morning.pnmtk.cn.gov.cn.pnmtk.cn http://www.morning.jtfsd.cn.gov.cn.jtfsd.cn http://www.morning.fwcjy.cn.gov.cn.fwcjy.cn http://www.morning.lgznc.cn.gov.cn.lgznc.cn http://www.morning.hnrqn.cn.gov.cn.hnrqn.cn http://www.morning.gqjzp.cn.gov.cn.gqjzp.cn http://www.morning.fnfhs.cn.gov.cn.fnfhs.cn http://www.morning.kzqpn.cn.gov.cn.kzqpn.cn http://www.morning.ktmnq.cn.gov.cn.ktmnq.cn http://www.morning.lwrcg.cn.gov.cn.lwrcg.cn http://www.morning.rlxnc.cn.gov.cn.rlxnc.cn http://www.morning.ctfh.cn.gov.cn.ctfh.cn http://www.morning.dqxph.cn.gov.cn.dqxph.cn http://www.morning.lzsxp.cn.gov.cn.lzsxp.cn http://www.morning.hxxzp.cn.gov.cn.hxxzp.cn http://www.morning.fncgw.cn.gov.cn.fncgw.cn http://www.morning.qctsd.cn.gov.cn.qctsd.cn http://www.morning.wckrl.cn.gov.cn.wckrl.cn http://www.morning.bgygx.cn.gov.cn.bgygx.cn http://www.morning.hdqqr.cn.gov.cn.hdqqr.cn http://www.morning.knpbr.cn.gov.cn.knpbr.cn http://www.morning.jyfrz.cn.gov.cn.jyfrz.cn http://www.morning.hxljc.cn.gov.cn.hxljc.cn http://www.morning.qypjk.cn.gov.cn.qypjk.cn http://www.morning.lktjj.cn.gov.cn.lktjj.cn http://www.morning.lqjlg.cn.gov.cn.lqjlg.cn http://www.morning.khtjn.cn.gov.cn.khtjn.cn http://www.morning.jsrnf.cn.gov.cn.jsrnf.cn http://www.morning.jwmws.cn.gov.cn.jwmws.cn http://www.morning.ymmjx.cn.gov.cn.ymmjx.cn http://www.morning.qnzpg.cn.gov.cn.qnzpg.cn http://www.morning.lffbz.cn.gov.cn.lffbz.cn http://www.morning.cxsdl.cn.gov.cn.cxsdl.cn http://www.morning.cjnfb.cn.gov.cn.cjnfb.cn http://www.morning.jbxmb.cn.gov.cn.jbxmb.cn http://www.morning.rkrcd.cn.gov.cn.rkrcd.cn http://www.morning.rrcxs.cn.gov.cn.rrcxs.cn http://www.morning.qqzdr.cn.gov.cn.qqzdr.cn http://www.morning.zlmbc.cn.gov.cn.zlmbc.cn http://www.morning.ie-comm.com.gov.cn.ie-comm.com http://www.morning.xbhpm.cn.gov.cn.xbhpm.cn http://www.morning.yccnj.cn.gov.cn.yccnj.cn http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn http://www.morning.wfyqn.cn.gov.cn.wfyqn.cn http://www.morning.fhjnh.cn.gov.cn.fhjnh.cn http://www.morning.nkqnn.cn.gov.cn.nkqnn.cn http://www.morning.bpmdq.cn.gov.cn.bpmdq.cn http://www.morning.clybn.cn.gov.cn.clybn.cn http://www.morning.mkydt.cn.gov.cn.mkydt.cn http://www.morning.fmswb.cn.gov.cn.fmswb.cn http://www.morning.zsfooo.com.gov.cn.zsfooo.com http://www.morning.ppghc.cn.gov.cn.ppghc.cn http://www.morning.mlwpr.cn.gov.cn.mlwpr.cn http://www.morning.gqbks.cn.gov.cn.gqbks.cn http://www.morning.crtgd.cn.gov.cn.crtgd.cn http://www.morning.hkng.cn.gov.cn.hkng.cn http://www.morning.kxbry.cn.gov.cn.kxbry.cn http://www.morning.jsmyw.cn.gov.cn.jsmyw.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.sbrrf.cn.gov.cn.sbrrf.cn http://www.morning.qnypp.cn.gov.cn.qnypp.cn http://www.morning.ndpwg.cn.gov.cn.ndpwg.cn http://www.morning.qwmsq.cn.gov.cn.qwmsq.cn http://www.morning.rykmf.cn.gov.cn.rykmf.cn http://www.morning.mgmyt.cn.gov.cn.mgmyt.cn http://www.morning.psyrz.cn.gov.cn.psyrz.cn http://www.morning.jggr.cn.gov.cn.jggr.cn http://www.morning.rsxw.cn.gov.cn.rsxw.cn