Android Ble简析

本帖最后由 Liu 于 2014-11-28 10:38 编辑

   蓝牙BLE设备是目前比较热门的设备。由于BLE有低功耗等特点,被广泛应用到身边的电子产品上。如智能手表、手环、防丢器等各种产品上。最近研究一下android上的ble应用开发。跟大家分享一下相关的内容。
   我的实现使用两台android手机做ble相关的通信,一台机器作为服务端接收发过来的消息,另一台作为客户端发送消息。
   [b]客户端基本流程如下:[/b]
   ![ble.png|506x601](upload://vXPPFdrDPTTl0WZbSBlzOv5137A.jpeg)

1.添加蓝牙相关权限

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

2.BLE设备扫描
使用BluetoothAdapter.startLeScan来扫描发现设备,这个方法需要参数BluetoothAdapter.LeScanCallback,所以还需要实现此回调方法,来获取扫描结果。
注意:BLE扫描耗电量比较大,尽可能缩短扫描时间。发现用户所需连接的设备后立即停止扫描

public class DeviceScanActivity extends ListActivity {
    private BluetoothAdapter mBluetoothAdapter;
    private boolean mScanning;
    private Handler mHandler;
    // 10秒后停止寻找.
    private static final long SCAN_PERIOD = 10000;
    ...
    private void scanLeDevice(final boolean enable) {
        if (enable) {
            // 经过预定扫描期后停止扫描
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mScanning = false;
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                }
            }, SCAN_PERIOD);
            mScanning = true;
            mBluetoothAdapter.startLeScan(mLeScanCallback);
        } else {
            mScanning = false;
            mBluetoothAdapter.stopLeScan(mLeScanCallback);
        }
        ...
    }
...
}

BLE扫描结果的接口,下面是BluetoothAdapter.LeScanCallback的实现。可以获取到扫描到设备的蓝牙名称和蓝牙地址等。

private LeDeviceListAdapter mLeDeviceListAdapter;
...
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
    new BluetoothAdapter.LeScanCallback() {
    @Override
    public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
        runOnUiThread(new Runnable() {
    @Override
    public void run() {
         device.getName();//获得扫描到设备名称
         device.getAddress();//获取设备蓝牙地址
        }
     });
   }
};

3.连接
先获取BluetoothAdapterd,再获取BluetoothDevice。根据扫描的蓝牙设备地址获取BluetoothDevice对象。使用BluetoothDevice.connectGatt进行连接。返回BluetoothGatt实例。
然后使用connectGatt( )方法进行链接。这个方法需要三个参数:一个Context对象,自动连接(boolean值,表示只要BLE设备可用是否自动连接到它),和BluetoothGattCallback调用。后面会详细介绍BluetoothGattCallback的实现。

BluetoothManager mBluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();
BluetoothDevice mDevice = mBluetoothAdapter.getRemoteDevice(address);

4.发现BLE设备服务

mBluetoothGatt.discoverServices();

5.发送Characteristic
发送消息通过读写Characteristic来完成,app完成与GATT服务端连接和发现services后,就可以读写 writeCharacteristic,每个服务和characteristic都有一个UUID来唯一确定,所有想要开发BLE必须知道你想要用哪个服务的那个characteristic也就是要知道对应的UUID。

private void writeCharacteristic(String writeValue) {
        BluetoothGattCharacteristic characteristic =
        getCharacteristic(CHARACTERISTIC_UUID);
        if (characteristic == null) return;
        characteristic.setValue(writeValue);
        mBluetoothGatt.writeCharacteristic(characteristic);
}
private void readCharacteristic() {
        BluetoothGattCharacteristic characteristic =
         getCharacteristic(CHARACTERISTIC_UUID);
        if (characteristic != null) mBluetoothGatt.readCharacteristic(characteristic);
}

6.BluetoothGattCallback实现
根据BluetoothGattCallback判断当前状态。获取是否连接成功、是否断开连接、读写Characteristic是否成功等。

private final BluetoothGattCallback mGattCallbacks = new BluetoothGattCallback() {
     @Override
     //获取连接状态方法,BLE设备连接上或断开时,会调用到此方
     public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
         if (DEBUG) Log.d(TAG, "onConnectionStateChange");
         if (status == BluetoothGatt.GATT_SUCCESS) {
         if (newState == BluetoothProfile.STATE_CONNECTED) {
             showMessage("Bluetooth LE connected");
        }
        else if (status == BluetoothProfile.STATE_DISCONNECTED) {
             showMessage("Bluetooth LE disconnected");
         }
      }
    }

     //成功发现设备的services时,调用此方法
     @Override
     public void onServicesDiscovered(BluetoothGatt gatt, int status) {
         if ((status == BluetoothGatt.GATT_SUCCESS) &&
         (mBluetoothGatt.getService(SERVICE_UUID) != null)) {
             showMessage("Discover service Successful !!!");
         }
      }

    //读写characteristic时会调用到以下方法
     @Override
     public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
         if ((status == BluetoothGatt.GATT_SUCCESS) &&
             (characteristic.getUuid().equals(CHARACTERISTIC_UUID))) {
                 showMessage(characteristic.getStringValue(0));
            }
     }

     @Override
     public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
          BluetoothGattCharacteristic mCharacteristic =
             getCharacteristic(CHARACTERISTIC_UUID);
            if ((status == BluetoothGatt.GATT_SUCCESS) &&
                (characteristic.getStringValue(0).equals(mCharacteristic.getStringValue(0)))) {
                showMessage("CharacteristicWrite Successful !!!");
            }
        }
    };

7.断开链接

mBluetoothGatt.disconnect();

以上是客户端(进行消息发送)的实现,下面介绍服务端实现方法。
服务端实现比较简单,首先创建BluetoothManager,使用openGattServer实例mGattServer。openGattServer需要一个BluetoothGattServerCallback回调。创建BluetoothGattService,再把BluetoothGattService添加到BluetoothGattServer中。

BluetoothManager  mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothGattServer mGattServer = mBluetoothManager.openGattServer(this, mCallbacks);
BluetoothGattService mService = createService();
mGattServer.addService(mService);

BluetoothGattServerCallback回调的实现,在这个回调里面可以获取到服务端发送过来的消息,连接状态等

private final BluetoothGattServerCallback mCallbacks = new BluetoothGattServerCallback() {
    @Override
    //获取连接状态方法,BLE设备连接上或断开时,会调用到此方
    public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
        if (DEBUG) Log.d(TAG, "onConnectionStateChange: newState=" + newState);
        if (status == BluetoothGatt.GATT_SUCCESS) {
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                mDevice = device;
                String devicename = device.getName();
                String address = device.getAddress();
                notifyConnected(devicename);
                beginNotification();
            } else if (status == BluetoothProfile.STATE_DISCONNECTED) {
                stopNotification();
                notifyDisconnected();
                mDevice = null;
            }
        }
    }

    //service添加成功会调用此方
    @Override
    public void onServiceAdded(int status, BluetoothGattService service) {
        if (DEBUG) Log.d(TAG, "onServiceAdded()");
        if (status == BluetoothGatt.GATT_SUCCESS) notifyServiceAdded();
    }

    //读写Characteristic,在此获得客户端发来的消息
    @Override
    public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId,BluetoothGattCharacteristic characteristic,
        boolean preparedWrite, boolean responseNeeded,int offset, byte[] value) {
        if (DEBUG) Log.d(TAG, "onCharacteristicWriteRequest: preparedWrite=" + preparedWrite);
            try{
                mCharacteristicString = new String(value); //客户端发来的消息
            }catch(Exception e){
            }
            notifyCharacteristicWriteRequest(mCharacteristicString);
        }
    }

     @Override
    public void onCharacteristicReadRequest(BluetoothDevice device, int requestId,
        int offset, BluetoothGattCharacteristic characteristic) {
        if (DEBUG) Log.d(TAG, "onCharacteristicReadRequest()");
            notifyCharacteristicReadRequest();
        }
};

android ble的内容就暂时介绍到这里,上面介绍的实现方法,是有一台android手机来模拟一个BLE的设备。拿到真实的BLE设备(防丢器),还会继续发帖分享给大家,希望大家继续关注!!

再发几张我这个测试应用的操作截图
1.在server端点击Start Server,上面文本提示BLE SERVICE ADDED


2.在Client端,输入服务端手机的蓝牙地址,然后点在onnect

3.进行配对,两台手机分别点击配对

4.客户端点击Discover service,Toast提示Discover service Successful。
服务端显示Device Connect。

5.客户端输入要发送的字符"123456",点击Write Characteristic。
服务端接收到发送过来的字符,显示Device Write:123456

6.客户端点击Disconnect断开连接
服务端显示Device Disconnected

楼主有心整理了,学习了!
Android BLE现在运用挺广泛的,低功耗市场很大,期待楼主有更深入的技术介绍
{:3_48:}

楼主威武呀!能否分享一下你用的是什么版本的android和机型…
我手里的小米只能当client,作为server无法发现,不知道啥原因了!

linjincn 发表于 2015-1-7 15:38
楼主威武呀!能否分享一下你用的是什么版本的android和机型…
我手里的小米只能当client,作为server无法 …

手机只能模拟成BLE设备,不能做真实的设备来使用。
模拟设备时可能出现扫描不到的情况。可以跳过扫描,直接通过手机的蓝牙地址进行链接使用。
手机是ANDROID4.4。

您好楼主,我在学习BLE,我用的是SONY的 手机,android 系统4.4.2 KitKat,BLE sensortag 在做。
mBluetoothAdapter.startLeScan(mLeScanCallback);
上面的这个方法在API 19已经不能使用了,然而我换用mBluetoothAdapter.startScan(mLeScanCallback);
也不行。
请帮忙解决一下!谢谢了。

楼主,我这里怎么也链接不上。能分享下代码吗?

楼主的app,能发出来试试吗