您的位置:首页 > 移动开发 > Android开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: