栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

python3 搜索附近蓝牙

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python3 搜索附近蓝牙

本来想用java写,奈何java太繁琐,python 功能包真丰富啊

依赖包
pip install -i https://mirrors.aliyun.com/pypi/simple/ pybluez
代码
# -*- coding: utf-8 -*-
"""
Created on Sat Nov  6 10:00:35 2021

@author: liuyunshengsir
"""

import bluetooth

print("开始搜索附近蓝牙...")

nearby_devices = bluetooth.discover_devices(duration=8, lookup_names=True,flush_cache=True, lookup_class=False)

print("找到 {} 个蓝牙设备".format(len(nearby_devices)))

for addr, name in nearby_devices:
    try:
        print("  mac地址 :{} - 蓝牙名称:{}".format(addr, name))
    except UnicodeEncodeError:
        print(" mac地址 :{} - 蓝牙名称:{}".format(addr, name.encode("utf-8", "replace")))

错误情况
Traceback (most recent call last):
   File "",line1,in 
AttributeError: 'module' object has no attribute 'discover_devices'

原因:不能以bluetooth.py命名或者运行目录含有bluetooth.py的文件
I had the same problem, I did the mistake of naming my program file bluetooth.py , which confused python for the package resolution, you should look out for this silly mistake if this is the case .

If this is not the case then try to put your file in the directory where your bluetooth directory (in my case, C:Python27Libsite-packages) is located and then run it from there, it worked for me.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/434284.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号