Android BLE开发

Android BLE简介

蓝牙低能耗(Bluetooth Low Energy,或称Bluetooth LE、BLE,旧商标Bluetooth Smart)也称低功耗蓝牙,是蓝牙技术联盟设计和销售的一种个人局域网技术,旨在用于医疗保健、运动健身、信标、安防、家庭娱乐等领域的新兴应用。相较经典蓝牙,低功耗蓝牙旨在保持同等通信范围的同时显著降低功耗和成本。。

流程

Created with Raphaël 2.2.0 开始 扫描 已绑定? 连接 读/写/通知 结束 绑定/配对 yes no

扫描

  • startDiscovery:扫传统蓝牙
java.lang.Object
   ↳	android.bluetooth.BluetoothAdapter

public boolean startDiscovery()

Returns
boolean	true on success, false on error

Added in API level 5

Requires Manifest.permission.BLUETOOTH_ADMIN

  • startLeScan:扫低功耗蓝牙
java.lang.Object
   ↳	android.bluetooth.BluetoothAdapter
   
public boolean startLeScan(UUID[] serviceUuids, 
                BluetoothAdapter.LeScanCallback callback)
                
Returns
boolean	true, if the scan was started successfully

Added in API level 18 
Deprecated in API level 21

This method was deprecated in API level 21.
use BluetoothLeScanner#startScan(List, ScanSettings, ScanCallback) instead.

Requires Manifest.permission.BLUETOOTH_ADMIN

  • startScan
java.lang.Object
   ↳	android.bluetooth.le.BluetoothLeScanner
   
public void startScan (List<ScanFilter> filters, 
                ScanSettings settings, 
                ScanCallback callback)

Added in API level 21

An app must hold ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in order to get results. 

Requires Manifest.permission.BLUETOOTH_ADMIN

If you are targeting SDK version 21+, User should enable location in settings for BLE to work.

绑定/配对

  • createBond
java.lang.Object
   ↳	android.bluetooth.BluetoothDevice
   
public boolean createBond ()

Returns
boolean	false on immediate error, true if bonding will begin

Added in API level 19

Start the bonding (pairing) process with the remote device.
This is an asynchronous call, it will return immediately.

Requires Manifest.permission.BLUETOOTH_ADMIN

  • setPin
java.lang.Object
   ↳	android.bluetooth.BluetoothDevice
   
public boolean setPin (byte[] pin)

Returns
boolean	true pin has been set false for error

Added in API level 19
Set the pin during pairing 

Requires Manifest.permission.BLUETOOTH_ADMIN.

连接

  • connectGatt
java.lang.Object
   ↳	android.bluetooth.BluetoothDevice
   
public BluetoothGatt connectGatt (Context context, 
                boolean autoConnect, 
                BluetoothGattCallback callback)

Added in API level 18

  • BluetoothGattCallback
java.lang.Object
   ↳	android.bluetooth.BluetoothGattCallback

public void onCharacteristicChanged (BluetoothGatt gatt, 
                BluetoothGattCharacteristic characteristic)
Callback triggered as a result of a remote characteristic notification.

public void onCharacteristicRead (BluetoothGatt gatt, 
                BluetoothGattCharacteristic characteristic, 
                int status)
Callback reporting the result of a characteristic read operation.

public void onCharacteristicWrite (BluetoothGatt gatt, 
                BluetoothGattCharacteristic characteristic, 
                int status)
Callback indicating the result of a characteristic write operation.

Added in API level 18

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值