Android低功耗蓝牙开发
<h1><strong>参考</strong></h1><p>https://developer.android.com/guide/topics/connectivity/bluetooth-le</p>
<p>https://www.jianshu.com/p/3a372af38103</p>
<h1><strong>简介</strong></h1>
<p>最近公司有个连接设备商蓝牙的小功能,于是把蓝牙相关的api简单过了一下,基本可以开发了。</p>
<p> </p>
<p>Android 4.3(api 18)引入了 蓝牙低功耗的支持,并提供了能够用来发现设备,查询service,传输信息的api。</p>
<p>当一个用户用他的设备 用ble和其他的设备配对时,两个设备间的数据传输是能被用户设备的所有app访问到的。因此,如果你的应用程序捕获敏感数据,你应该实现自己的应用层的协议来保护这些数据的隐私。</p>
<p> </p>
<h1><strong>开发流程</strong></h1>
<h2><strong>权限声明</strong></h2>
<p>首先你需要声明bluetooth权限,这个权限是蓝牙的基础权限,其他的操作蓝牙都需要先声明此权限。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">uses-permission </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="android.permission.BLUETOOTH"</span><span style="color: rgba(0, 0, 255, 1)">/></span></pre>
</div>
<p> </p>
<p>如果你要使用蓝牙扫描周围设备(BluetoothAdapter.startDiscovery,或BluetoothLeScanner.startScan),或者操作蓝牙的设置,你就需要此权限。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">uses-permission </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="android.permission.BLUETOOTH_ADMIN"</span><span style="color: rgba(0, 0, 255, 1)">/></span></pre>
</div>
<p> </p>
<p>另外,如果你的app是运行在Android8.0以下的设备上时,因为可以被发现的设备可能会暴露其位置,你需要ACCESS_FINE_LOCATION权限,</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">uses-permission </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="android.permission.ACCESS_FINE_LOCATION"</span> <span style="color: rgba(0, 0, 255, 1)">/></span></pre>
</div>
<ul>
<li>此权限是危险权限,所以需要运行时动态申请。</li>
<li>既然是访问定位,那么系统的定位功能必须的打开才行。</li>
</ul>
<p> </p>
<p>如果你的app是运行在Android8.0+的设备上时,你可以使用<span style="text-decoration: underline">CompanionDeviceManager的api</span>,CompanionDeviceManager将代表您的应用程序对附近设备执行蓝牙或Wi-Fi扫描,而无需访问ACCESS_FINE_LOCATION或BLUETOOTH_ADMIN权限。当然如果不使用CompanionDeviceManager的话就需要按上边的申请权限了。</p>
<p> </p>
<p>如果你的app是需要支持ble的设备才能运行,就需要在清单文件中声明,</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">uses-feature </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="android.hardware.bluetooth_le"</span><span style="color: rgba(255, 0, 0, 1)"> android:required</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(0, 0, 255, 1)">/></span></pre>
</div>
<p>但如果你也想让不支持ble的设备也能运行,也需要上边的声明,并把required设置为false,但你需要在代码中自行判断:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<p> </p>
<h2><strong>启动蓝牙</strong></h2>
<p>1. 在Android4.3通过getSystemService获取</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">fina</span>BluetoothManager bluetoothManager =<span style="color: rgba(0, 0, 0, 1)"> (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter </span>= bluetoothManager.getAdapter();</pre>
</div>
<p> </p>
<p>2. 然后启用蓝牙</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">if</span> (bluetoothAdapter == <span style="color: rgba(0, 0, 255, 1)">nul</span>|| !<span style="color: rgba(0, 0, 0, 1)">bluetoothAdapter.isEnabled()) {
Intent enableBtIntent </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}</span></pre>
</div>
<p>之后会启动一个系统弹窗,如下图:</p>
<p><img src="https://img2020.cnblogs.com/blog/661881/202012/661881-20201214144242640-963503883.jpg" alt="wps13" width="359" height="142" title="wps13" border="0" style="display: inline; background-image: none"></p>
<p>REQUEST_ENABLE_BT是我们自定义的>=0,之后会在onActivityForResult中收到启动结果,RESULT_OK表示启动成功,RESULT_CANCELED表示取消。</p>
<p> </p>
<h2><strong>获取当前已配对的外围设备</strong></h2>
<p>第一次和外部蓝牙设备连接时,配对请求就会自动的显示给用户,当配对成功后,设备的基本信息(如设备名,设备类型,mac地址等信息)就会被存储到手机中,可以通过getBondedDevices(需要先开启蓝牙)访问到。</p>
<p>之后再次连接时,就可以直接使用已配对的已知的mac地址直接和远程设备建立连接,而不需要先进行扫描(并不是真的不扫描,只是我们不用手动调用),当然前提时外围设备在我们能扫描到的范围内。</p>
<p> </p>
<p>配对和连接的区别:</p>
<ul>
<li>配对是两个设备之间互相存储了对方的设备信息,以及一个沟通的密钥。</li>
<li>连接是两个设备间共享同一个RFCOMM通道,可以进行互传数据。在Android api中,在连接之前会自动的先配对。</li>
</ul>
<p> </p>
<p>所以在扫描之前有必要先获取当前已配对的设备,看有没有我们想要进行连接的设备。</p>
<div class="cnblogs_code">
<pre>Set<BluetoothDevice> pairedDevices =<span style="color: rgba(0, 0, 0, 1)"> bluetoothAdapter.getBondedDevices();
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (pairedDevices.size() > 0<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> There are paired devices. Get the name and address of each paired device.</span>
<span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (BluetoothDevice device : pairedDevices) {
String deviceName </span>=<span style="color: rgba(0, 0, 0, 1)"> device.getName();
String deviceHardwareAddress </span>= device.getAddress(); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> MAC address</span>
<span style="color: rgba(0, 0, 0, 1)"> }
}</span></pre>
</div>
<p> </p>
<h2><strong>扫描ble设备</strong></h2>
<p>如果已配对的设备没有我们想要的,那么再进行扫描周围的蓝牙设备。</p>
<p> </p>
<p>您只能扫描Bluetooth LE设备 或 传统蓝牙设备,如Bluetooth中所述。 您不能同时扫描Bluetooth LE和传统设备。</p>
<p> </p>
<p>因为扫描是一个耗电的操作,所以您应该遵守以下准则:</p>
<ul>
<li>一旦你找到期望的设备就应该停止扫描。</li>
<li>切勿循环扫描,并为扫描设置时间限制。 先前可用的设备可能已超出范围,并且继续扫描会耗尽电池电量。</li>
</ul>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">private</span> BluetoothLeScanner bluetoothLeScanner =<span style="color: rgba(0, 0, 0, 1)">
BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> mScanning;
</span><span style="color: rgba(0, 0, 255, 1)">private</span> Handler handler = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Handler();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Stops scanning after 10 seconds.</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">long</span> SCAN_PERIOD = 10000<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> scanLeDevice() {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">mScanning) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Stops scanning after a pre-defined scan period.</span>
handler.postDelayed(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Runnable() {
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> run() {
mScanning </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
bluetoothLeScanner.<span style="color: rgba(255, 0, 0, 1)">stopScan</span>(leScanCallback);
}
}, SCAN_PERIOD);
mScanning </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
bluetoothLeScanner.<span style="color: rgba(255, 0, 0, 1)">startScan</span>(leScanCallback);
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
mScanning </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
bluetoothLeScanner.stopScan(leScanCallback);
}
}</span></pre>
</div>
<ul>
<li>bluetoothLeScanner.startScan有几个重载方法,都是用来过滤扫描到的设备的。</li>
</ul>
<p>startScan(ScanCallback)是不进行过滤,那么当屏幕关闭时为了省电会暂停扫描,屏幕再打开时恢复扫描,为了防止这样,请使用带有过滤的startScan。</p>
<p> </p>
<ul>
<li>bluetoothLeScanner.stopScan时必须要传入和startScan传入的callback一样的callback才能停止。</li>
</ul>
<p> </p>
<p>ScanCallback:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> LeDeviceListAdapter leDeviceListAdapter;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Device scan callback.</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> ScanCallback leScanCallback =
<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ScanCallback() {
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onScanResult(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> callbackType, ScanResult result) {
</span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onScanResult(callbackType, result);
leDeviceListAdapter.addDevice(result.getDevice());
leDeviceListAdapter.notifyDataSetChanged();
}
};</span></pre>
</div>
<ul>
<li><span style="color: rgba(255, 0, 0, 1)">此回调是在主线程的。</span></li>
<li>callbackType:表示这个回调是如何触发的,它是在ScanSettings中配置的callbackType,</li>
<li>ScanResult包含了下边几个重要数据:
<ul>
<li>BluetoothDevice:扫描到的蓝牙设备的抽象,可以用此对象进行配对连接。</li>
<li>int Rssi:received signastrength in dBm,接收到的此蓝牙设备的信号强度</li>
<li>ScanRecord:表示扫描到的蓝牙设备的广播数据,</li>
</ul>
</li>
</ul>
<p> </p>
<h2><strong>连接GATT server</strong></h2>
<p>和传统蓝牙的连接不同的是,我们可以使用已经封装好的Android api来进行连接ble设备,而不用再去创建通道,然后建立socket等操作。</p>
<p>与BLE设备交互的第一步是连接到它——更具体地说,连接到设备上的GATT服务器。</p>
<p> </p>
<p>在第一次连接的时候会弹 一个系统弹窗 或 通知,让用户进行配对,</p>
<p><img src="https://img2020.cnblogs.com/blog/661881/202012/661881-20201214144243422-1725135714.jpg" alt="wps14" width="253" height="160" title="wps14" border="0" style="display: inline; background-image: none"></p>
<p> </p>
<p> </p>
<p>要连接到BLE设备上的GATT服务器,可以使用BluetoothDevice.connectGatt()方法。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> BluetoothGatt connectGatt(Context context, <span style="color: rgba(0, 0, 255, 1)">boolean</span> autoConnect, BluetoothGattCallback callback)</pre>
</div>
<ul>
<li>autoConnect:表示是否在BLE设备可用时自动连接。</li>
</ul>
<p>关于autoConnect参数为true的意义?</p>
<p>在蓝牙核心文档Vol3: Core System Package->Part C: Generic Access Profile的Connection Modes and Procedures章节中有涉及到自动连接建立规程(Auto Connection Establishment Procedure)的定义。</p>
<p>自动连接建立规程用来向多个设备同时发起连接。一个中央设备的主机与多个外围设备绑定,只要它们开始广播,便立刻与其建立连接。跟多细节请参考蓝牙核心文档和协议栈源码。</p>
<p> </p>
<ul>
<li>BluetoothGattCallback :是用来把一些信息返回给GATT client,比如连接状态的改变,GATT client的一些操作的结果。<span style="color: rgba(255, 0, 0, 1)">所有的回调都是后台线程上的。</span></li>
</ul>
<p> </p>
<p> </p>
<p>当调用蓝牙的连接方法之后,蓝牙会异步执行蓝牙连接的操作,如果连接成功会回调 BluetoothGattCalbackl#onConnectionStateChange 方法。这个方法运行的线程是一个 Binder 线程,所以不建议直接在这个线程处理耗时的任务,因为这可能导致蓝牙相关的线程被阻塞。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onConnectionStateChange(BluetoothGatt gatt, <span style="color: rgba(0, 0, 255, 1)">int</span> status, <span style="color: rgba(0, 0, 255, 1)">int</span> newState)</pre>
</div>
<ul>
<li>status:代表是否成功执行了连接操作,如果为 BluetoothGatt.GATT_SUCCESS 表示成功执行连接操作,第三个参数才有效,否则说明这次连接尝试不成功。有时候,我们会遇到 status == 133 的情况,根据网上大部分人的说法,这是因为 Android 最多支持连接 6 到 7 个左右的蓝牙设备,如果超出了这个数量就无法再连接了。所以当我们断开蓝牙设备的连接时,还必须调用 BluetoothGatt#close 方法释放连接资源。否则,在多次尝试连接蓝牙设备之后很快就会超出这一个限制,导致出现这一个错误再也无法连接蓝牙设备。</li>
</ul>
<p> </p>
<ul>
<li>newState:代表当前设备的连接状态,如果 newState == BluetoothProfile.STATE_CONNECTED 说明设备已经连接,可以进行下一步的操作了(发现蓝牙服务,也就是 Service)。当蓝牙设备断开连接时,这一个方法也会被回调,其中的 newState == BluetoothProfile.STATE_DISCONNECTED。</li>
</ul>
<p> </p>
<h2><strong>获取service</strong></h2>
<p>1. 在连接成功后,可以通过BluetoothGatt.discoverServices()来查找外围设备上支持的service,</p>
<p>2. 此操作是异步操作,之后会在 BluetoothGattCalbackl#onServicesDiscovered回调:</p>
<p>3. 然后我们可以通过BluetoothGatt.getService(UUID uuid)来获取我们期望的service。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">final</span> BluetoothGattCallback gattCallback =
<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BluetoothGattCallback() {
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onConnectionStateChange(BluetoothGatt bluetoothGatt, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> status,
</span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> newState) {
String intentAction;
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (newState ==<span style="color: rgba(0, 0, 0, 1)"> BluetoothProfile.STATE_CONNECTED) {
bluetoothGatt.discoverServices();
} </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (newState ==<span style="color: rgba(0, 0, 0, 1)"> BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, </span>"Disconnected from GATT server."<span style="color: rgba(0, 0, 0, 1)">);
}
}
@Override
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> New services discovered</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onServicesDiscovered(BluetoothGatt bluetoothGatt, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> status) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (status ==<span style="color: rgba(0, 0, 0, 1)"> BluetoothGatt.GATT_SUCCESS) {
bluetoothGatt.getService(uuid);
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
Log.w(TAG, </span>"onServicesDiscovered received: " +<span style="color: rgba(0, 0, 0, 1)"> status);
}
}
@Override
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Result of a characteristic read operation</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, </span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> status) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (status ==<span style="color: rgba(0, 0, 0, 1)"> BluetoothGatt.GATT_SUCCESS) {
}
}
};</span></pre>
</div>
<p> </p>
<h2><strong>获取Charactristic数据</strong></h2>
<p>接着通过 BluetoothGattService#getCharacteristic(UUID uuid)、getCharacteristics()获取 BluetoothGattCharacteristic。</p>
<p>此时获取的别没有Characteristic的value,只有uuid信息。调用BluetoothGatt.readCharacteristic来读取,之后会在BluetoothGattCallback.onCharacteristicRead回调:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onCharacteristicRead(<span style="color: rgba(0, 0, 255, 1)">final</span><span style="color: rgba(0, 0, 0, 1)"> BluetoothGatt gatt,
</span><span style="color: rgba(0, 0, 255, 1)">final</span><span style="color: rgba(0, 0, 0, 1)"> BluetoothGattCharacteristic characteristic,
</span><span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> status) {
Log.d(TAG, </span>"callback characteristic read status " +<span style="color: rgba(0, 0, 0, 1)"> status
</span>+ " in thread " +<span style="color: rgba(0, 0, 0, 1)"> Thread.currentThread());
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (status ==<span style="color: rgba(0, 0, 0, 1)"> BluetoothGatt.GATT_SUCCESS) {
Log.d(TAG, </span>"read value: " +<span style="color: rgba(0, 0, 0, 1)"> characteristic.getValue());
}
}
BluetoothGattService service </span>=<span style="color: rgba(0, 0, 0, 1)"> gattt.getService(SERVICE_UUID);
BluetoothGattCharacteristic characteristic </span>=<span style="color: rgba(0, 0, 0, 1)"> gatt.getCharacteristic(CHARACTER_UUID);
gatt.readCharacteristic(characteristic);</span></pre>
</div>
<p> </p>
<h2><strong>向Charactristic写入数据</strong></h2>
<p>1. 调用 BluetoothGattCharactristic#setValue 传入需要写入的数据(蓝牙最多单次支持 20 个字节数据的传输,如果需要传输的数据大于这一个字节则需要<span style="text-decoration: underline">分包传输</span>)。</p>
<p>2. 调用 BluetoothGattCharactristic#writeCharacteristic 方法通知系统异步往设备写入数据。</p>
<p>3. 系统回调 BluetoothGattCallback#onCharacteristicWrite 方法通知数据已经完成写入。此时,我们需要执行 BluetoothGattCharactristic#getValue 方法检查一下写入的数据是否我们需要发送的数据,如果不是按照项目的需要判断是否需要重发。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onCharacteristicWrite(<span style="color: rgba(0, 0, 255, 1)">final</span><span style="color: rgba(0, 0, 0, 1)"> BluetoothGatt gatt,
</span><span style="color: rgba(0, 0, 255, 1)">final</span><span style="color: rgba(0, 0, 0, 1)"> BluetoothGattCharacteristic characteristic,
</span><span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> status) {
Log.d(TAG, </span>"callback characteristic write in thread " +<span style="color: rgba(0, 0, 0, 1)"> Thread.currentThread());
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(!<span style="color: rgba(0, 0, 0, 1)">characteristic.getValue().equal(sendValue)) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 执行重发策略</span>
<span style="color: rgba(0, 0, 0, 1)"> gatt.writeCharacteristic(characteristic);
}
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">往蓝牙数据通道的写入数据</span>
BluetoothGattService service =<span style="color: rgba(0, 0, 0, 1)"> gattt.getService(SERVICE_UUID);
BluetoothGattCharacteristic characteristic </span>=<span style="color: rgba(0, 0, 0, 1)"> gatt.getCharacteristic(CHARACTER_UUID);
characteristic.setValue(sendValue);
gatt.writeCharacteristic(characteristic);</span></pre>
</div>
<p> </p>
<h3><strong>超过20字节分包问题</strong></h3>
<p><span style="text-decoration: underline">https://stackoverflow.com/questions/24135682/android-sending-data-20-bytes-by-ble</span></p>
<p>做法是:</p>
<ol>
<li>发送第一个20字节后,在onCharacteristicWrite接收到发送成功回调,</li>
<li>然后在接着发下一个20字节,然后再在onCharacteristicWrite接收到发送成功回调,循环如此直到发完为止。</li>
</ol>
<p><span style="color: rgba(255, 0, 0, 1)">另外可能和ble设备性能有关,有的ble设备在发送完上次20字节后需要等待几十毫秒(sleep),才能再发。</span></p>
<p> </p>
<h2><strong>监听characteristic 的改变</strong></h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> BluetoothGatt bluetoothGatt;
BluetoothGattCharacteristic characteristic;
</span><span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> enabled;
...
bluetoothGatt.setCharacteristicNotification(characteristic, enabled);
...
BluetoothGattDescriptor descriptor </span>=<span style="color: rgba(0, 0, 0, 1)"> characteristic.getDescriptor(
UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);</span></pre>
</div>
<p>值得注意的是,除了通过 BluetoothGatt#setCharacteristicNotification 开启 Android 端接收通知的开关,</p>
<p>还需要往 Characteristic 的 Descriptor 属性写入开启通知的数据开关使得当硬件的数据改变时,主动往手机发送数据。</p>
<p> </p>
<p>之后如果监听的characteristic的值发生改变,就会在BluetoothGattCallback.onCharacteristicChanged(gatt, characteristic)接收到。</p>
<p> </p>
<h2><strong>断开连接</strong></h2>
<p>当我们连接蓝牙设备完成一系列的蓝牙操作之后就可以断开蓝牙设备的连接了。</p>
<p>1. BluetoothGatt#disconnect</p>
<p>通过 BluetoothGatt#disconnect 可以断开正在连接的蓝牙设备。当这一个方法被调用之后,系统会异步回调 BluetoothGattCallback#onConnectionStateChange 方法。通过这个方法的 newState 参数可以判断是连接成功还是断开成功的回调。</p>
<p> </p>
<p>2. BluetoothGatt#close</p>
<p>由于 Android 蓝牙连接设备的资源有限,当我们执行断开蓝牙操作之后必须执行 BluetoothGatt#close 方法释放资源。</p>
<p>需要注意的是通过 BluetoothGatt#close 方法也可以执行断开蓝牙的操作,不过 BluetoothGattCallback#onConnectionStateChange 将不会收到任何回调。此时如果执行 BluetoothGatt#connect 方法会得到一个蓝牙 API 的空指针异常。所以,我们推荐的写法是当蓝牙成功连接之后,通过 BluetoothGatt#disconnect 断开蓝牙的连接,紧接着在 BluetoothGattCallback#onConnectionStateChange 执行 BluetoothGatt#close 方法释放资源。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onConnectionStateChange(<span style="color: rgba(0, 0, 255, 1)">final</span> BluetoothGatt gatt, <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> status,
</span><span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> newState) {
Log.d(TAG, </span>"onConnectionStateChange: thread "
+ Thread.currentThread() + " status " +<span style="color: rgba(0, 0, 0, 1)"> newState);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (status !=<span style="color: rgba(0, 0, 0, 1)"> BluetoothGatt.GATT_SUCCESS) {
String err </span>= "Cannot connect device with error status: " +<span style="color: rgba(0, 0, 0, 1)"> status;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当尝试连接失败的时候调用 disconnect 方法是不会引起这个方法回调的,所以这里
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 直接回调就可以了。</span>
<span style="color: rgba(0, 0, 0, 1)"> gatt.close();
Log.e(TAG, err);
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (newState ==<span style="color: rgba(0, 0, 0, 1)"> BluetoothProfile.STATE_CONNECTED) {
gatt.discoverService();
} </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (newState ==<span style="color: rgba(0, 0, 0, 1)"> BluetoothProfile.STATE_DISCONNECTED) {
gatt.close();
}
}</span></pre>
</div>
<p> </p>
<p> </p>
<h2><strong>监听蓝牙的状态的改变</strong></h2>
<p>你可以监听BluetoothAdapter.ACTION_STATE_CHANGED广播,当系统蓝牙的开关状态发生改变时会通知你,</p>
<p>广播包含两个int数据,</p>
<ul>
<li>BluetoothAdapter.EXTRA_STATE:现在的state</li>
<li>BluetoothAdapter.EXTRA_PREVIOUS_STATE:表示之前的state</li>
</ul>
<p> </p>
<p>state的取值如下:</p>
<ul>
<li>BluetoothAdapter.STATE_TURNING_ON</li>
<li>BluetoothAdapter.STATE_ON</li>
<li>BluetoothAdapter.STATE_TURNING_OFF</li>
<li>BluetoothAdapter.STATE_OFF</li>
</ul>
<p> </p>
<h1><strong>其他方法</strong></h1>
<h2><strong>获取已连接的蓝牙设备</strong></h2>
<ul>
<li><s>直接通过bluetoothManager.getConnectedDevices是获取不到的,</s><s></s></li>
<li><s>通过</s><s>bluetoothManager.getConnectionState</s><s>也不行</s><s></s></li>
<li>通过反射获取,</li>
</ul>
<div class="cnblogs_code">
<pre>List<BluetoothDevice> connectedDevices = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">();
Class</span><BluetoothAdapter> bluetoothAdapterClass = BluetoothAdapter.<span style="color: rgba(0, 0, 255, 1)">class</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">得到BluetoothAdapter的Class对象</span>
<span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> getConnectionState方法是获取设备是否和任何一个蓝牙设备连接</span>
Method method = bluetoothAdapterClass.getDeclaredMethod("getConnectionState", (Class[]) <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
method.setAccessible(</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">int</span> state = (<span style="color: rgba(0, 0, 255, 1)">int</span>) method.invoke(bluetoothAdapter, (Object[]) <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (state ==<span style="color: rgba(0, 0, 0, 1)"> BluetoothAdapter.STATE_CONNECTED) {
Log.i(</span>"BLUETOOTH", "BluetoothAdapter.STATE_CONNECTED"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 已连接的设备肯定已经配对过了,所以从这里直接获取已配对的设备</span>
Set<BluetoothDevice> devices =<span style="color: rgba(0, 0, 0, 1)"> bluetoothAdapter.getBondedDevices();
</span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (BluetoothDevice device : devices) {
Method isConnectedMethod </span>= BluetoothDevice.<span style="color: rgba(0, 0, 255, 1)">class</span>.getDeclaredMethod("isConnected", (Class[]) <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
method.setAccessible(</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">boolean</span> isConnected = (<span style="color: rgba(0, 0, 255, 1)">boolean</span>) isConnectedMethod.invoke(device, (Object[]) <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (isConnected) {
Log.i(</span>"BLUETOOTH", "connected:" +<span style="color: rgba(0, 0, 0, 1)"> device.getName());
connectedDevices.add(device);
}
}
}
} </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
e.printStackTrace();
}</span></pre>
</div>
<p> </p>
<h1><strong>问题</strong></h1>
<h2><strong>startDiscovery</strong><strong>、startScan的区别</strong></h2>
<p><span style="text-decoration: underline">android - BluetoothAdapter.startScan() vs BluetoothAdapter.startLeScan() - Stack Overflow</span></p>
<p> </p>
<ul>
<li>startDiscovery()用来扫描 传统蓝牙设备,</li>
<li>startLeScan()用来扫描 低功耗蓝牙设备。</li>
</ul>
<p>对蓝牙适配器来说,执行设备发现是一个繁重的过程,将消耗大量资源。</p>
<p>Edit:</p>
<p>On LG Nexus 4 with Android 4.4.2 startDiscovery() finds Bluetooth LE devices.</p>
<p>On Samsung Galaxy S3 with Android 4.3 startDiscovery() doesn't find Bluetooth LE devices.</p>
<p> </p>
<h1><strong>API常见错误码</strong></h1>
<p><span style="text-decoration: underline">https://www.cnblogs.com/Free-Thinker/p/11507349.html</span></p>
<p>android上层api并没有把所有的连接status都给公开(BluetoothGatt中),所以有时会收到api中没有的status,下边是几个可能会遇到的:</p>
<ul>
<li>GATT_ERROR 0x85 //133任何不惧名字的错误都出现这个错误码,出现了就认怂吧,重新连接吧。</li>
<li>GATT_CONN_TIMEOUT 0x08 //8 连接超时,大多数情况是设备离开可连接范围,然后手机端连接超时断开返回此错误码。</li>
<li>GATT_CONN_TERMINATE_PEER_USER 0x13 //19 连接被对端设备终止,直白点就是手机去连接外围设备,外围设备任性不让连接执行了断开。</li>
<li>GATT_CONN_TERMINATE_LOCAL_HOST 0x16 //22 连接被本地主机终止,可以解释为手机连接外围设备,但是连接过程中出现一些比如鉴权等问题,无法继续保持连接,主动执行了断开操作。</li>
<li>GATT_CONN_FAIL_ESTABLISH 03E //62 连接建立失败。</li>
</ul>
<h1>封装好的库</h1>
<p>在做功能时顺便也做了个蓝牙工具库的module,之后上传到gayhub上再来更新地址。</p>
<p> </p>
<h2><strong>其他第三方库</strong></h2>
<p>https://github.com/Jasonchenlijian/FastBle</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/muouren/p/14133081.html
頁:
[1]