Skip to content

Instantly share code, notes, and snippets.

@binderclip
Created April 4, 2016 08:29
Show Gist options
  • Save binderclip/8389fc41b6262cd438e14f2db76ed78e to your computer and use it in GitHub Desktop.
Save binderclip/8389fc41b6262cd438e14f2db76ed78e to your computer and use it in GitHub Desktop.
蓝牙原理的了解(杂乱的笔记)

是有看一本专门介绍蓝牙的书,看过之后对蓝牙基本的东西有了一些了解,就能轻松的知道它能干什么不能干什么,适合干什么,不适合干什么。

  • client
  • server

initiates a connection

outgoing choose a target device transport protocol

incoming choose a transport protocol listen before accepting

Outgoing

  • choose a target device

    • search for nearby devices
    • query each device for its display name
    • choose device with user-specified name
  • choose a transport protocol and port number

    • hard-code a protocol
    • search target device for SDP records matching a predefined identifier
    • choose port numbet on matching record
  • establish a connection

    • socket(...)
    • connect(...)
  • transfer data

    • send(...), recv(...)
  • disconnect

    • close(...)
    • difference
    • initial process of choosing a target device
    • transport protocol
    • port number

Incoming

  • Choose a transport protocol and port number
    • hard-code a protocol
    • choose a hard-coded or dynamically assigned port number
  • reserve local resources and enter listening mode
    • socket(...)
    • bind(...)
    • listen(...)
  • [EMPTY]
    • Advertise service with local SDP server (optional, but recommended)
  • wait for and accept incoming connections
    • listen(...)
    • accept(...)
  • transfer data
    • send(...), recv(...)
  • disconnect
    • close(...)

Choosing a Target Device

  • MAC Address, 48-bit
  • Device Name
    • user-friendly
    • name arbitrary
      • 可能重复,如果重复的话再说了
  • Searching for Nearby Devices
    • broadcast “discovery”
    • 只有被扫描的时候回复说我我我,不能主动去告诉别人
  • Inquiry Scan
    • Discoverability
  • Page Scan
    • Connectability

Choosing a Transport Protocol

  • reliable: TCP...

    • streaming
  • best-effort: UCP...

    • packet
    • 但是简单处理一下也能够变成 streaming 的
  • RFCOMM

    • streaming
    • 类似 TCP
    • 为了模拟 RS-232 而造!所以本身就是为了 RS-232 啊
    • 但是应该说它是通用的,但是刚好可以很好的支持 RS-232
    • 只有 30 个 Port
    • 一般都是用这个!有的平台甚至只支持这个,但是这个本身就很好用的,所以不担心
  • L2CAP

    • packet
    • 类似 UDP
    • 但是比 UDP 要靠谱
    • 顺序是确保的
    • 可以设置不同等级的重发机制
    • RFCOMM 内部用的也是 L2CAP
  • ACL

    • 更底层一些,上面的会用到它
    • 我们一般不会用到它
    • 两个蓝牙设备之间只能有一个 ACL 链路
  • SCO

    • best-effort
    • 64 kb/s, phone call OK, mp3 not OK (may use L2CAP)
    • 这个主要是蓝牙耳机再用,其他的地方一般不会用

reserved/well-known ports

  • 1-1023 ports
  • SDP 1
  • 1-4095 中的奇数可能会使用到
  • 4097–32,765 可以自定义使用

Service Discovery Protocol

是可以靠 port 1 的 SDP 来动态分配端口的

  • service record
    • Service ID and Service Class ID List
  • service id
    • 虽然描述很好用,但是找到时候还是用唯一的 service id 来查找
    • service id 的空间很大,比 port id 大得多,所以设计的时候就去制定一个 service id 吧!
    • 使用 128-bit UUID
    • usage
      • server 设置好这个 service id (UUID)
      • client search for this to connect
  • service class id list
    • 额外的一个标签列表
    • 可以用于单个应用支持多个协议
    • 或者两个类似的东西可以支持一个相同的协议
  • bluetooth reserved UUIDs
    • SDP
    • RFCOMM
    • L2CAP
    • ...
  • common SDP attributes
    • 一些常用的 SDP 的属性,如前面所说,主要是用来指明是做什么的
  • SDP Record Structure - Detail
    • 暂时不用去了解这个

Communicating via Sockets

  • introducing the socket
    • client/server 只是说 outgoing 和 incoming,并不是在应用层里面真的 server 是服务器
  • client sockets
  • server/listening sockets
    • bind
    • listen
    • accept 会创建出新的 socket 用来通信
    • 本身的 server socket 是不参与通信的
    • 完了之后会 close
    • Nonblocking Sockets with select
      • 这里其实是使用 event loop 的方式来统筹的处理一个一个事件,从而实现异步的效果
      • event 里面调用的处理函数应该是很快返回、不阻塞的才行
      • 这里使用的是 select 的方法

useful things to know about bluetooth

communications range

  • 100m
  • 10m
  • 1m

人体(有很多水)就会吸收 60% 的蓝牙信号

communications speed

radio frequencies and channel hopping

bluetooth networks - piconets, scatternets, masters and slavese

security - PINs and link keys

  • pairing
    • giving a PIN
    • hard coding or prompt user to input
    • use PIN to generate a link key
    • PIN 是不会在空气中直接传播的!
    • 配对之后就可以请求验证和加密了
    • 如果应用需要加密的话不要过分依赖自带的加密,但是对一般的来说还是很有用的
  • security mode
    • 三个等级,之后再说
  • simple pairing
    • 有的设备本身是不能改变自己的 PIN 的,比如蓝牙耳机,那么久 hard code 成 0000 吧,但是这样的话,不好啊所以干脆用这个 2.1 里面的协议吧,不需要手动 PIN 的,只需要是否连接就好了

bluetooth profiles + RFCs

  • OBEX Object Push 传送小文件
  • File Transfer 可以做文件操作
  • Dial-Up Networking 可以让其他设备使用设备的蓝牙来无线上网
  • Hands-Free Audio 打电话,使用 SCO
  • Advanced audio Distribution
  • Personal Area Network 当做 IP 层的东西来用,而且可以分享网络
  • Human Interface Device 蓝牙键盘之类的东西
  • Serial Port Profile RFCOMM 并且当做串口线来使用

Host Controller Interface

是 computer 和它的 local Bluetooth adapters 之间的通信

Limitations - Things Bluetooth Can't Do

因为本身的协议的一些限制,有些看似简单的事情 Bluetooth 是做不了的,除非到时候增加新的标准来实现(道理说起来就是这么的简单啊)

Device Discovery Explained

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment