Bluetooth identification and bluepy Bluetooth serial port testing in Ubuntu


Preface

In Ubuntu systems, Bluetooth debugging is often required. The following introduces how to use a Bluetooth adapter (CSR8510).

系统 :Ubuntu 16.04
python3 版本 : python3
USB2.0蓝牙适配器 :CSR8510

1. Bluetooth adapter

1. Bluetooth adapter identification

  • Plug in the hardware, open a terminal and see if the device is detected:
╰─➤  lsusb
Bus 001 Device 003: ID 203a:fffa
Bus 001 Device 002: ID 203a:fffa
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 002: ID 203a:fff9
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 004: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)
Bus 002 Device 003: ID 203a:fffe
Bus 002 Device 002: ID 203a:fffc
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

  • Check whether it is recognized as a Bluetooth module:
╰─➤  hciconfig -a
hci0:	Type: BR/EDR  Bus: USB
	BD Address: 00:1A:7D:DA:71:13  ACL MTU: 310:10  SCO MTU: 64:8
	UP RUNNING PSCAN
	RX bytes:1264 acl:0 sco:0 events:55 errors:0
	TX bytes:2432 acl:0 sco:0 commands:47 errors:0
	Features: 0xff 0xff 0x8f 0xfe 0xdb 0xff 0x5b 0x87
	Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
	Link policy: RSWITCH HOLD SNIFF PARK
	Link mode: SLAVE ACCEPT
	Name: 'ubuntu'
	Class: 0x00010c
	Service Classes: Unspecified
	Device Class: Computer, Laptop
	HCI Version: 4.0 (0x6)  Revision: 0x22bb
	LMP Version: 4.0 (0x6)  Subversion: 0x22bb
	Manufacturer: Cambridge Silicon Radio (10)
  • View the address of the Bluetooth module:
╰─➤  hcitool dev
Devices:
	hci0	00:1A:7D:DA:71:13

2. Bluetooth adapter activation

  • When debugging BLE devices, you need to obtain root permissions.
#设备打开蓝牙
sudo hciconfig hci0 up
#设备关闭蓝牙
sudo hciconfig hci0 down

2. bluepy test

1. bluepy installation

1. 安装libbluetooth-dev
sudo apt-get install libbluetooth-dev
2. 安装bluepy
sudo python3 -m pip install bluepy 

说明:本次安装bluepy 版本为 1.3.0 ,可通过指令查看
ls /usr/local/lib/python3.5/dist-packages

2. bluepy test

  • Scan demo.py as follows:
# #!/usr/bin/python
# -*- coding: UTF-8 -*-
import requests
import os
import tempfile
import errno
import sys
import binascii
import unittest
import warnings
import argparse
import time
from bs4 import BeautifulSoup
from binascii import hexlify
from time import gmtime, strftime, sleep
from bluepy import btle
from bluepy.btle import Scanner, DefaultDelegate,UUID, Peripheral

class ScanDelegate(DefaultDelegate):
    def __init__(self):
        DefaultDelegate.__init__(self)

    def handleDiscovery(self, dev, isNewDev, isNewData):
        if isNewDev:
            print("Discovered device", dev.addr)
        elif isNewData:
            print("Received new data from", dev.addr)


class NotifyDelegate(DefaultDelegate):
    def __init__(self):
        DefaultDelegate.__init__(self)

    def handleNotification(self, cHandle, data):
        print("Notification from Handle: 0x" + format(cHandle,'02X') )
        print(hexlify(data))

def ScannerDevice(devices,devAddr):
    for dev in devices:
        if dev.addr==devAddr:
            targetDev = dev
            print("扫描到目标设备: %s" %(dev.addr))

        print("---------------")
        print("Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi))
        for (adtype, desc, value) in dev.getScanData():
            print("  %s = %s" % (desc, value))
        
        if dev.addr==devAddr:
            print("停止继续扫描")
            print("---------------")
            break


if __name__ == '__main__':
    addr = "00:17:c7:9c:2f:6b"
    scanner = Scanner().withDelegate(ScanDelegate())
    devices = scanner.scan(10.0)
    ScannerDevice(devices,addr.lower())
    print("总共扫描到 %d 设备" %(len(devices)))
  • Get Bluetooth Mac address:
1. 安卓系统可通过指令获取:
adb shell settings get secure bluetooth_address
22:22:F4:FA:0E:00

2.其他目标设备自行查找:
当前测试设备蓝牙Mac : 00:17:c7:9c:2f:6b
  • Execute demo.py:
sudo python3 scan.py
  • As shown in the following log, you can view the scanned device information:
╰─➤  sudo python3 demo.py
Discovered device 78:c9:b7:a1:80:b1
Discovered device 46:93:fa:63:9e:2f
Discovered device 00:d0:d0:01:af:e7
Discovered device 00:17:c7:9c:2f:6b
Discovered device 22:f6:b6:86:83:e7
Discovered device 3d:c6:1f:b5:aa:21
Discovered device 60:12:92:34:05:47
Discovered device 00:17:c7:e9:aa:d3
Discovered device 6a:dd:3a:63:39:03
Discovered device 48:58:86:17:48:1b
Discovered device 2e:f1:bb:0f:84:b2
Discovered device 9c:30:5b:0f:96:dc
Discovered device 50:32:37:9a:f0:68
Discovered device 6e:63:37:a5:a5:fa
Discovered device 0a:80:a0:d3:ee:cc
Discovered device eb:37:57:9a:6f:bd
---------------
Device 48:58:86:17:48:1b (random), RSSI=-52 dB
  Flags = 1a
  Tx Power = 08
  Manufacturer = 4c001006311ef32f5e61
---------------
Device 60:12:92:34:05:47 (random), RSSI=-48 dB
  Flags = 06
  Manufacturer = 4c001005411c9acfe2
---------------
Device 0a:80:a0:d3:ee:cc (random), RSSI=-57 dB
  Flags = 1b
  Manufacturer = 4c000906036dc0a80144
---------------
Device 2e:f1:bb:0f:84:b2 (random), RSSI=-57 dB
  Manufacturer = 0600010f202268ba9931cf4a88fc30007040e6e6492f4c0d3441fe423c
---------------
Device 00:17:c7:e9:aa:d3 (public), RSSI=-56 dB
  Flags = 06
  Incomplete 16b Services = 0000fe59-0000-1000-8000-00805f9b34fb
  Complete 16b Services = 00006cce-0000-1000-8000-00805f9b34fb
  16b Service Data = ce6c06230017c7e9aad30000000000000000000000000001
  Complete Local Name = DfuTarg
---------------
Device 00:d0:d0:01:af:e7 (random), RSSI=-54 dB
  Manufacturer = 0600010920024336d7803f26bb3a485a061a223cc094bb3b3033e5176c
---------------
Device 6e:63:37:a5:a5:fa (random), RSSI=-54 dB
  Flags = 1a
  Tx Power = 08
  Manufacturer = 4c001006641e95c510e7
---------------
Device 22:f6:b6:86:83:e7 (random), RSSI=-52 dB
  Manufacturer = 0600010920022dc2fa2a969120dff439c0193fae099a1fb594b54423f1
---------------
Device eb:37:57:9a:6f:bd (random), RSSI=-54 dB
  Manufacturer = 4c0012020001
扫描到目标设备: 00:17:c7:9c:2f:6b
---------------
Device 00:17:c7:9c:2f:6b (public), RSSI=-56 dB
  Flags = 06
  Complete 16b Services = 00006cce-0000-1000-8000-00805f9b34fb
  16b Service Data = ce6c03c00017c79c2f6b00160000000000000000000000001d
  Manufacturer = 4c00021570dc44c3e2a84b22a2c6129b41a4bdbcc79c2f6bc5
停止继续扫描
---------------
总共扫描到 16 设备

Summarize

The above is what I will talk about today. This article only briefly introduces the use of bluepy. Setting the target Bluetooth Mac address can obtain relevant information by executing the script.

Guess you like

Origin blog.csdn.net/chenkanuo/article/details/132194386