最近在做一个蓝牙的项目,是Blu4.0,Android系统支持是4.3版本以上(为此特地卖了个手机,)。
当然也参考了不少大牛的的微博,说的都不错,再次特地感谢
http://blog.csdn.net/hellogv/article/details/6036849
http://blog.csdn.net/hellogv/article/details/6042091
http://www.cnblogs.com/zdz8207/archive/2012/10/17/bluetooth_ble_android.html
http://blog.csdn.net/changemyself/article/details/8454633
http://blog.csdn.net/hellogv/article/details/24267685
调试Ble硬件的读写的App
http://www.wandoujia.com/apps/com.siflink.ble.mgr
这个App,可以在自己app为开发好的情况下,简单的测试下读写特征值,不知道为什么,好像只能写入一个值。也许是我不知道怎么写入多个值。
下面这个是从github上搜索的一个ble4.0的代码
https://github.com/StevenRudenko/BleSensorTag
网上有很多关于Ble4.0的开发,再次也不做介绍,反正都是差不多的。
1、声明蓝牙的权限
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
2、监听搜索的回调
BleBluetoothGattCallback gattCallback = new BleBluetoothGattCallback();
gattCallback.setOnBleReadCallback(new OnBleReadCallback() {
public void onReadData(byte[] data) { } public void onReadComple() {
McLog.mByStackTrace();
} public void onConnect() {
showToast("连接设备成功");
isConnect = true;
} public void onDisConnect() {
showToast("断开设备");
isConnect = false;
}
});
BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
McLog.mByStackTrace();
String address = device.getAddress();
String name = device.getName();
String t = name + "[" + address + "]";
McLog.i("find a device %s", t); if (isRightDevice(device)) { // 判断是否是自己的想要的设备
btAdapter.stopLeScan(this);
// 进行请求的链接
device.connectGatt(context, false, gattCallback);
McLog.i("device %s is this account's.", t);
} else {
McLog.i("device %s is not this account's.", t);
}
}
}; BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
btAdapter = bluetoothManager.getAdapter();
btAdapter.startLeScan(leScanCallback);
3、设置请求链接的回调
public class BleBluetoothGattCallback extends BluetoothGattCallback { public static final UUID SERVER_UUID = UUID.fromString("0000FFF0-0000-1000-8000-00805F9B34FB"); OnBleReadCallback onBleReadCallback; public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { // 链接状态的改变的回调
McLog.mByStackTrace();
McLog.i("status = " + status);
McLog.i("newState = " + newState);
switch (newState) {
case BluetoothProfile.STATE_CONNECTED:
McLog.e("STATE_CONNECTED"); // 链接成功
gatt.discoverServices();
if (onBleReadCallback != null) {
onBleReadCallback.onConnect();
}
break;
case BluetoothProfile.STATE_DISCONNECTED:
gatt.close();
McLog.e("STATE_DISCONNECTED");// 断开链接
if (onBleReadCallback != null) {
onBleReadCallback.onDisConnect();
}
break;
case BluetoothProfile.STATE_CONNECTING:
McLog.e("STATE_CONNECTING");
break;
case BluetoothProfile.STATE_DISCONNECTING:
McLog.e("STATE_DISCONNECTING");
break;
}
} public void onServicesDiscovered(final BluetoothGatt gatt, int status) { // <span style="font-family: Arial, Helvetica, sans-serif;">发现服务的回调</span>
McLog.mByStackTrace();
McLog.i("status = " + status);
if (status == BluetoothGatt.GATT_SUCCESS) {
BluetoothGattService service = gatt.getService(SERVER_UUID);
McLog.i("service = " + service);
if (service != null) {
// 找到服务后,可以进行读取数据和写入数据
}
} else {
// McToastUtil.show("discover services fail.");
}
} public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {// 读取特征值的回调
McLog.i("onCharacteristicRead");
UUID currentUUId = characteristic.getUuid();
McLog.i("currentUUId = " + currentUUId);
if (currentUUId == null) {
return;
}
// 数据的内容
byte[] data = characteristic.getValue();
McLog.i("data = " + Arrays.toString(data));
} public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {// 写入特征值回调
McLog.i("onCharacteristicWrite status = " + status);
} public void setOnBleReadCallback(OnBleReadCallback onBleReadCallback) {
this.onBleReadCallback = onBleReadCallback;
} public interface OnBleReadCallback { // 自己定义的回调 void onConnect(); void onReadData(byte[] data); void onReadComple(); void onDisConnect();
}
}
最后特别重要的是,一定好配合硬件工程师调试蓝牙。