最近在作一個藍牙的項目,是Blu4.0,Android系統支持是4.3版本以上(爲此特意賣了個手機,)。html
固然也參考了很多大牛的的微博,說的都不錯,再次特意感謝java
http://blog.csdn.net/hellogv/article/details/6036849android
http://blog.csdn.net/hellogv/article/details/6042091git
http://www.cnblogs.com/zdz8207/archive/2012/10/17/bluetooth_ble_android.html
github
http://blog.csdn.net/changemyself/article/details/8454633
app
http://blog.csdn.net/hellogv/article/details/24267685
測試
調試Ble硬件的讀寫的Appui
http://www.wandoujia.com/apps/com.siflink.ble.mgrthis
這個App,能夠在本身app爲開發好的狀況下,簡單的測試下讀寫特徵值,不知道爲何,好像只能寫入一個值。也許是我不知道怎麼寫入多個值。spa
下面這個是從github上搜索的一個ble4.0的代碼
https://github.com/StevenRudenko/BleSensorTag
網上有不少關於Ble4.0的開發,再次也不作介紹,反正都是差很少的。
一、聲明藍牙的權限
<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" />
二、監聽搜索的回調
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);
三、設置請求連接的回調
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(); } }
最後特別重要的是,必定好配合硬件工程師調試藍牙。