Android 蓝牙4.0 BLE调试

配备低功耗蓝牙的可穿戴式设备应该是未来的一大趋势。

继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 plain copy
  1. mBtAdapter = BluetoothAdapter.getDefaultAdapter();  
  2.     if (!mBtAdapter.isEnabled()) {  
  3.         Log.i(TAG, "onClick - BT not enabled yet");  
  4.         Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
  5.         startActivityForResult(enableIntent, REQUEST_ENABLE_BT);  
  6.     }  
  7.     else {  
  8.         Intent newIntent = new Intent(HRPCollectorActivity.this, DeviceListActivity.class);  
  9.         startActivityForResult(newIntent, REQUEST_SELECT_DEVICE);  
  10.     }  

[java]  view plain copy
  1. Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();  
  2. for (BluetoothDevice pairedDevice : pairedDevices) {  
  3.     boolean result = false;  
  4.     result = mService.isBLEDevice(pairedDevice);  
  5.     if (result == true) {  
  6.         addDevice(pairedDevice, 0);  
  7.     }  
  8. }  


2. 使能Service

要取得与设备的蓝牙通信,必须先Configure该数据类型的Service Profile。

[java]  view plain copy
  1. public static final UUID ACC_MEASUREMENT_CHARAC = UUID.fromString("F000AA51-0451-4000-B000-000000000000");  
  2. public static final UUID ACC_MEASUREMENT_CHARAC2 = UUID.fromString("F000AA52-0451-4000-B000-000000000000");  
  3.   
  4.     BluetoothGattService mHRP = mBluetoothGatt.getService(device, ACC_SERVICE);  
  5.     BluetoothGattCharacteristic mHRCPcharac2 = mHRP.getCharacteristic(ACC_MEASUREMENT_CHARAC2);  
  6.     value[0] = (byte0x01;  
  7.     mHRCPcharac2.setValue(value[0], BluetoothGattCharacteristic.FORMAT_UINT8, 0);  
  8.     mBluetoothGatt.writeCharacteristic(mHRCPcharac2);  
  9.     BluetoothGattCharacteristic mHRMcharac = mHRP.getCharacteristic(ACC_MEASUREMENT_CHARAC);  
  10.     mBluetoothGatt.setCharacteristicNotification(mHRMcharac, true);  

3. 获取数据

[java]  view plain copy
  1. /** 
  2.  * GATT client callbacks 
  3.  */  
  4. private BluetoothGattCallback mGattCallbacks = new BluetoothGattCallback() {  
  5.     public void onCharacteristicRead(BluetoothGattCharacteristic charac, int status) {  
  6.         if (charUuid.equals(ACC_MEASUREMENT_CHARAC)) {  
  7.             byte[] accname = charac.getValue();  
  8.         }  


参考资料:http://blog.csdn.net/ooakk/article/details/7302425

来源:http://blog.csdn.net/qianjin0703/article/details/9995619

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值