android 蓝牙4.0 ble 的理解
2015-01-22 23:06
295 查看
环境:android 4.3 api18
参考:官方的BluetoothleGatt sample 和xpg的GizwitsBLE
记录核心的内容,憧憬物联网
权限:
scan:
BluetoothLeService: 里面包含了所有对ble设备的操作
onUnbind:
connect:
disconnect:
close:
readCharacteristic:
writeCharacteristic:
setCharacteristicNotification:
BluetoothGattCallback:
control:
写信息给设备:
注意的地方:
1. 发送和接收的UUID可以是不同的,这个要跟设备厂商协调
2. 发送数据的指令和格式,即协议,是设备厂商定的,需要文档支持
3. 不用的时候记得调用close(), 不让就会发现蓝牙搜索不到设备的情况。可以让连接的手机重启或清理缓存等操作解决
最后附上自己的理解,也许不太对:
ble就是一堆作为server的设备,把信息散发出来在某些通道,然后用手机等东西作为client去连接他们,交互发生在这些通道上,一个server只能被一个client连接
参考:官方的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();才能调用下面的connectGatt mBluetoothGatt = 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有什么动作的时候都到这里来,所有的交互信息 onConnectionStateChange onServicesDiscovered onCharacteristicRead onCharacteristicChanged onCharacteristicWrite</span>
control:
<span style="font-size:18px;">// 广播处理service传出来的数据交互信息 BroadcastReceiver ACTION_GATT_CONNECTED ACTION_GATT_DISCONNECTED // 这里要注意调用mBluetoothLeService.close();让disconnected后可以重新connect ACTION_GATT_SERVICES_DISCOVERED // 发现到ble设备的sevices的时候,调用使能语句 // setCharacteristicNotification(xx, true)使能某个UUID ACTION_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连接
相关文章推荐
- Android 蓝牙4.0(BLE) 理解--紧随智能家居和可穿戴设备潮流
- Android 蓝牙4.0 BLE 理解
- Android蓝牙4.0 ble开发初步理解 (一)
- Android 蓝牙4.0 BLE 理解
- Android 蓝牙4.0 BLE 理解
- Android 蓝牙4.0 BLE 理解
- Android 蓝牙4.0 BLE 理解
- Android Bluetooth蓝牙开发\蓝牙协议\蓝牙通信例子_Android支持蓝牙4.0版本_BLE开发
- Android Bluetooth蓝牙开发\蓝牙协议\蓝牙通信例子_Android支持蓝牙4.0版本_BLE开发
- android ble 蓝牙4.0 cc2540 开发(一)
- Android Bluetooth蓝牙开发\蓝牙协议\蓝牙通信例子_Android支持蓝牙4.0版本_BLE开发
- android 蓝牙4.0 ble 低功耗蓝牙
- 浅谈android蓝牙4.0BLE开发
- Android 【蓝牙4.0 BLE 低功耗可穿戴设备】 开发总结
- Android 蓝牙4.0 BLE
- Android ble 蓝牙4.0总结——基础版
- android蓝牙4.0(BLE)开发之ibeacon初步
- Android Bluetooth蓝牙开发\蓝牙协议\蓝牙通信例子_Android支持蓝牙4.0版本_BLE开发
- Android ble 蓝牙4.0 总结
- Android Bluetooth蓝牙开发\蓝牙协议\蓝牙通信例子_Android支持蓝牙4.0版本_BLE开发