Android 蓝牙4.0 BLE调试
2013-08-21 15:38
232 查看
配备低功耗蓝牙的可穿戴式设备应该是未来的一大趋势。
继IOS去年支持蓝牙BLE之后,Android最新的4.3版本也有了官方的支持。
受软硬件限制,目前的尝试基于第三方SDK。
Host端:支持蓝牙4.0的GS4手机,配合三星的蓝牙BLE开发包 http://developer.samsung.com/ble
Device端:支持蓝牙BLE的单片机CC2540,配合TI开发板 http://processors.wiki.ti.com/index.php/SensorTag_User_Guide
目标:手机监测CC2540的距离和方向
1. 设备匹配
与普通蓝牙设备的匹配类似,区别在于要判断是否是BLE设备。
[java] view
plaincopy
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBtAdapter.isEnabled()) {
Log.i(TAG, "onClick - BT not enabled yet");
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}
else {
Intent newIntent = new Intent(HRPCollectorActivity.this, DeviceListActivity.class);
startActivityForResult(newIntent, REQUEST_SELECT_DEVICE);
}
[java] view
plaincopy
Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
for (BluetoothDevice pairedDevice : pairedDevices) {
boolean result = false;
result = mService.isBLEDevice(pairedDevice);
if (result == true) {
addDevice(pairedDevice, 0);
}
}
2. 使能Service
要取得与设备的蓝牙通信,必须先Configure该数据类型的Service Profile。
[java] view
plaincopy
public static final UUID ACC_MEASUREMENT_CHARAC = UUID.fromString("F000AA51-0451-4000-B000-000000000000");
public static final UUID ACC_MEASUREMENT_CHARAC2 = UUID.fromString("F000AA52-0451-4000-B000-000000000000");
BluetoothGattService mHRP = mBluetoothGatt.getService(device, ACC_SERVICE);
BluetoothGattCharacteristic mHRCPcharac2 = mHRP.getCharacteristic(ACC_MEASUREMENT_CHARAC2);
value[0] = (byte) 0x01;
mHRCPcharac2.setValue(value[0], BluetoothGattCharacteristic.FORMAT_UINT8, 0);
mBluetoothGatt.writeCharacteristic(mHRCPcharac2);
BluetoothGattCharacteristic mHRMcharac = mHRP.getCharacteristic(ACC_MEASUREMENT_CHARAC);
mBluetoothGatt.setCharacteristicNotification(mHRMcharac, true);
3. 获取数据
[java] view
plaincopy
/**
* GATT client callbacks
*/
private BluetoothGattCallback mGattCallbacks = new BluetoothGattCallback() {
public void onCharacteristicRead(BluetoothGattCharacteristic charac, int status) {
if (charUuid.equals(ACC_MEASUREMENT_CHARAC)) {
byte[] accname = charac.getValue();
}
参考资料:http://blog.csdn.net/ooakk/article/details/7302425
来源:http://blog.csdn.net/qianjin0703/article/details/9995619
继IOS去年支持蓝牙BLE之后,Android最新的4.3版本也有了官方的支持。
受软硬件限制,目前的尝试基于第三方SDK。
Host端:支持蓝牙4.0的GS4手机,配合三星的蓝牙BLE开发包 http://developer.samsung.com/ble
Device端:支持蓝牙BLE的单片机CC2540,配合TI开发板 http://processors.wiki.ti.com/index.php/SensorTag_User_Guide
目标:手机监测CC2540的距离和方向
1. 设备匹配
与普通蓝牙设备的匹配类似,区别在于要判断是否是BLE设备。
[java] view
plaincopy
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBtAdapter.isEnabled()) {
Log.i(TAG, "onClick - BT not enabled yet");
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}
else {
Intent newIntent = new Intent(HRPCollectorActivity.this, DeviceListActivity.class);
startActivityForResult(newIntent, REQUEST_SELECT_DEVICE);
}
[java] view
plaincopy
Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
for (BluetoothDevice pairedDevice : pairedDevices) {
boolean result = false;
result = mService.isBLEDevice(pairedDevice);
if (result == true) {
addDevice(pairedDevice, 0);
}
}
2. 使能Service
要取得与设备的蓝牙通信,必须先Configure该数据类型的Service Profile。
[java] view
plaincopy
public static final UUID ACC_MEASUREMENT_CHARAC = UUID.fromString("F000AA51-0451-4000-B000-000000000000");
public static final UUID ACC_MEASUREMENT_CHARAC2 = UUID.fromString("F000AA52-0451-4000-B000-000000000000");
BluetoothGattService mHRP = mBluetoothGatt.getService(device, ACC_SERVICE);
BluetoothGattCharacteristic mHRCPcharac2 = mHRP.getCharacteristic(ACC_MEASUREMENT_CHARAC2);
value[0] = (byte) 0x01;
mHRCPcharac2.setValue(value[0], BluetoothGattCharacteristic.FORMAT_UINT8, 0);
mBluetoothGatt.writeCharacteristic(mHRCPcharac2);
BluetoothGattCharacteristic mHRMcharac = mHRP.getCharacteristic(ACC_MEASUREMENT_CHARAC);
mBluetoothGatt.setCharacteristicNotification(mHRMcharac, true);
3. 获取数据
[java] view
plaincopy
/**
* GATT client callbacks
*/
private BluetoothGattCallback mGattCallbacks = new BluetoothGattCallback() {
public void onCharacteristicRead(BluetoothGattCharacteristic charac, int status) {
if (charUuid.equals(ACC_MEASUREMENT_CHARAC)) {
byte[] accname = charac.getValue();
}
参考资料:http://blog.csdn.net/ooakk/article/details/7302425
来源:http://blog.csdn.net/qianjin0703/article/details/9995619
相关文章推荐
- Android 蓝牙4.0 BLE调试
- 蓝牙4.0——Android BLE开发官方文档翻译
- [iOS]在xcode的iOS虚拟机中对BLE(蓝牙4.0)进行调试
- Android 蓝牙4.0 BLE
- android 蓝牙4.0 ble 的理解
- Android 调试助手ATools [WIFI BT NFC GPS USB BLE 蓝牙串口 iBeacon ......]
- Android 蓝牙4.0 BLE 理解
- Android蓝牙4.0BLE
- Android ble 蓝牙4.0 总结
- 蓝牙4.0——Android BLE开发官方文档翻译
- [iOS]在xcode的iOS虚拟机中对BLE(蓝牙4.0)进行调试
- Android 蓝牙4.0 Ble通讯问题小汇总
- Android 蓝牙4.0(BLE)开发实现对蓝牙的写入数据和读取数据
- Android 蓝牙4.0BLE开发实现对蓝牙的写入数据和读取数据
- Android ble 蓝牙4.0 总结一
- android蓝牙4.0(BLE)开发之ibeacon初步
- Android Bluetooth蓝牙开发\蓝牙协议\蓝牙通信例子_Android支持蓝牙4.0版本_BLE开发
- Android蓝牙4.0 ble开发初步理解 (一)
- 分享Android 蓝牙4.0(ble)开发的解决方案
- Android蓝牙4.0 BLE开发坑总结