当前位置: 代码迷 >> Android >> android 蓝牙4.0 ble 的懂得
  详细解决方案

android 蓝牙4.0 ble 的懂得

热度:11   发布时间:2016-04-28 02:38:35.0
android 蓝牙4.0 ble 的理解
环境:android 4.3 api18
参考:官方的BluetoothleGatt sample 和xpg的GizwitsBLE
记录核心的内容,憧憬物联网

权限:
<span style="font-size:18px;"><uses-permission android:name="android.permission.BLUETOOTH"/><uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/><uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>android:minSdkVersion="18"</span>

scan:

<span style="font-size:18px;">// 获取到蓝牙适配器final BluetoothManager bluetoothManager =                (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);mBluetoothAdapter = bluetoothManager.getAdapter();// 适配器搜索的回调private BluetoothAdapter.LeScanCallback mLeScanCallback =            new BluetoothAdapter.LeScanCallback() {        @Override        public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {                   }    };// 使用适配器进行搜索mBluetoothAdapter.startLeScan(mLeScanCallback);// 停止搜索mBluetoothAdapter.stopLeScan(mLeScanCallback);</span>

BluetoothLeService: 里面包含了所有对ble设备的操作

onUnbind: 
<span style="font-size:18px;">mBluetoothGatt.close();</span>

connect:
<span style="font-size:18px;">// 真正连接设备的地方,这里不能重复调,要确保调用了mBluetoothGatt.close();才能调用下面的connectGattmBluetoothGatt = device.connectGatt(this, false, mGattCallback);</span>

disconnect:
<span style="font-size:18px;">mBluetoothGatt.disconnect();</span>

close:
<span style="font-size:18px;">mBluetoothGatt.close();</span>

readCharacteristic:
<span style="font-size:18px;">// 读service里面的characteristic,ble设备会发送信息到某个的UUID,mBluetoothGatt.readCharacteristic(characteristic);</span>

writeCharacteristic:
<span style="font-size:18px;">// 写信息给设备mBluetoothGatt.writeCharacteristic(characteristic);</span>

setCharacteristicNotification:
<span style="font-size:18px;">// 使能某个UUID地址,一旦这个地址有数据的变化就会通知我们mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);</span>

BluetoothGattCallback:
<span style="font-size:18px;">// ble的回调,当ble有什么动作的时候都到这里来,所有的交互信息onConnectionStateChangeonServicesDiscoveredonCharacteristicReadonCharacteristicChangedonCharacteristicWrite</span>

control:
<span style="font-size:18px;">// 广播处理service传出来的数据交互信息BroadcastReceiverACTION_GATT_CONNECTEDACTION_GATT_DISCONNECTED  // 这里要注意调用mBluetoothLeService.close();让disconnected后可以重新connectACTION_GATT_SERVICES_DISCOVERED  // 发现到ble设备的sevices的时候,调用使能语句                                                             //  setCharacteristicNotification(xx, true)使能某个UUIDACTION_DATA_AVAILABLE // 当这个使能的通道数据有变化的时候,就会调用这里</span>

写信息给设备:
<span style="font-size:18px;">// 获取指定Service里的指定Characteristic, 然后在这个Characteristic上把信息写进去BluetoothGattCharacteristic characteristic = mBluetoothLeService.getService(                        SERVICE_UUID).getCharacteristic(UUID);    characteristic.setValue(value);mBluetoothLeService.writeCharacteristic(characteristic);</span>

注意的地方:
1. 发送和接收的UUID可以是不同的,这个要跟设备厂商协调
2. 发送数据的指令和格式,即协议,是设备厂商定的,需要文档支持
3. 不用的时候记得调用close(), 不让就会发现蓝牙搜索不到设备的情况。可以让连接的手机重启或清理缓存等操作解决

最后附上自己的理解,也许不太对:
ble就是一堆作为server的设备,把信息散发出来在某些通道,然后用手机等东西作为client去连接他们,交互发生在这些通道上,一个server只能被一个client连接

  相关解决方案