BleOperationUtil

public class BleOperationUtil {

private static final StringTAG ="BleOperationUtil";

    private ContextmContext;

    private BluetoothAdaptermBluetoothAdapter;

    private BluetoothGattmBluetoothGatt;

    private BleOperationImBleOperationI;

    public BleOperationUtil(Context context, BleOperationI bleOperationI) {

mContext = context;

        mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();

        mBleOperationI = bleOperationI;

    }

/**

* Start connect bluetooth device

    * @param device bluetooth device

*/

    public void deviceConnect(BluetoothDevice device) {

if ( !device.connectGatt(mContext, false, mGattCallback).connect()) {

mBleOperationI.onConnectFail();

        }

}

/**

* Through the MAC connection.

*/

    public void macDeviceConnect(String mac) {

if (mBluetoothAdapter ==null || mac ==null) {

Log.d(TAG, "mBluetoothAdapter|mac is null;check bluetooth whether open");

            mBleOperationI.onConnectFail();

return;

        }

BluetoothDevice remoteDevice =mBluetoothAdapter.getRemoteDevice(mac);

        if (remoteDevice ==null) {

Log.d(TAG, "BluetoothDevice is null;Check if your equipment is not online");

            mBleOperationI.onConnectFail();

return;

        }

remoteDevice.connectGatt(mContext, false, mGattCallback);

        if (mBluetoothGatt ==null) {

Log.d(TAG, "BluetoothGatt is null");

            mBleOperationI.onConnectFail();

        }

}

/**

* Open or close notify passageway.

*

    * @param isOpen Open or close Indication

*/

    public void enableNotify(String serviceUuid, String notifyUuid, boolean isOpen, boolean isNotify ){

BluetoothGattService service = getService(UUID.fromString(serviceUuid));

        if (service ==null) {

Log.d(TAG, "service==null;check your service uuid please.");

            mBleOperationI.onNotifyFail();

return;

        }

BluetoothGattCharacteristic gattCharacteristic =service.getCharacteristic(UUID.fromString(notifyUuid));

        if (gattCharacteristic ==null) {

Log.d(TAG, "service==null;check your notify uuid please.");

            mBleOperationI.onNotifyFail();

return;

        }

//Default descriptorUUID:00002902-0000-1000-8000-00805f9b34fb

        BluetoothGattDescriptor descriptor =gattCharacteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));

        if (descriptor ==null) {

Log.d(TAG, "descriptor==null;check your descriptor uuid please.");

            mBleOperationI.onNotifyFail();

return;

        }

if (isOpen) {

if (isNotify){

descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

            }else {

descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);

            }

}else {

descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);

        }

if (!mBluetoothGatt.writeDescriptor(descriptor)) {

Log.d(TAG, "Write Descriptor fail");

            mBleOperationI.onNotifyFail();

        }else {

Log.d(TAG, "enableIndication true" );

            mBluetoothGatt.setCharacteristicNotification(gattCharacteristic, true);

        }

}

/**

* Read device characteristic.

*/

    public void readCharacteristic(String serviceUuid, String readUuid) {

BluetoothGattService service = getService(UUID.fromString(serviceUuid));

        if (service ==null) {

Log.d(TAG, "service is null,check you service uuid");

            mBleOperationI.onRCFail();

return;

        }

//得到此服务结点下Characteristic对象

        final BluetoothGattCharacteristic gattCharacteristic =service.getCharacteristic(UUID.fromString(readUuid));

        if (gattCharacteristic ==null) {

Log.d(TAG, "gattCharacteristic is null,check your read uuid");

            mBleOperationI.onRCFail();

return;

        }

if (mBluetoothAdapter ==null ||mBluetoothGatt ==null) {

Log.d(TAG, "mBluetoothAdapter/mBluetoothGatt == null,check your bluetooth connect state");

            mBleOperationI.onConnectFail();

return;

        }

if (!mBluetoothGatt.readCharacteristic(gattCharacteristic)) {

Log.d(TAG, "Read characteristic Fail");

            mBleOperationI.onRCFail();

        }

}

public void writeCharacteristic(String serviceUUID, String characteristicUUID, byte[] value) {

BluetoothGattService service =mBluetoothGatt.getService(UUID.fromString(serviceUUID));

        if (service ==null) {//设备为空__或者设备在远处

            mBleOperationI.onConnectDisconnected();//断开连接

            return;

        }

BluetoothGattCharacteristic characteristic =service.getCharacteristic(UUID.fromString(characteristicUUID));

        if (!characteristic.setValue(value)) {

return;

        }

if (mBluetoothGatt.writeCharacteristic(characteristic)) {

mBleOperationI.onWriteCharSuccess(new String(value));

            Log.d(TAG, "writeCharacteristic:true"+"\n"+new String(value));

        }else {

Log.d(TAG, "writeCharacteristic:false");

        }

}

//Disconnect and clean gatt property

    public synchronized void cleanBluetoothGatt() {

if (mBluetoothGatt ==null) {

return;

        }

if (mBluetoothGatt.connect()) {

mBluetoothGatt.disconnect();

            mBluetoothGatt =null;

        }

}

private BluetoothGattServicegetService(UUID uuid) {

if (mBluetoothAdapter ==null ||mBluetoothGatt ==null) {

Log.d(TAG, "mBluetoothAdapter/mBluetoothGatt==null;check your bluetooth connect state please");

            mBleOperationI.onConnectFail();

return null;

        }

return mBluetoothGatt.getService(uuid);

    }

BluetoothGattCallbackmGattCallback =new BluetoothGattCallback() {

//Characteristic read.

    @Override

    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {

StringBuilder stringBuilder =new StringBuilder();

        for (byte b:

characteristic.getValue()) {

stringBuilder.append(b+" ");

        }

Log.d(TAG, "onCharacteristicRead:" +stringBuilder);

        mBleOperationI.onRCResult(characteristic.getValue());

    }

//Notify read.

    @Override

    public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {

super.onDescriptorRead(gatt, descriptor, status);

        mBleOperationI.onNotifyResult(descriptor.getValue());

    }

@Override

    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {

super.onCharacteristicWrite(gatt, characteristic, status);

    }

@Override

    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {

Log.d(TAG, "onCharacteristicChanged:\n" +new String(characteristic.getValue()));

        mBleOperationI.onCharacteristicChanged(characteristic.getValue());

        super.onCharacteristicChanged(gatt, characteristic);

    }

@Override

    public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {

Log.d(TAG, "onReliableWriteCompleted:" + status);

        super.onReliableWriteCompleted(gatt, status);

    }

@Override

    public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {

super.onMtuChanged(gatt, mtu, status);

    }

};

    public interface BleOperationI {

/**

* 读取特性成功回调。

*/

        void onRCResult(byte[] data);

        /**

* 读取特性失败回调。

*/

        void onRCFail();

        /**

* 通知回调。

*/

        void onNotifyResult(byte[] data);

        /**

* 通知失败回调

*/

        void onNotifyFail();

        /**

* 特征码改变回调

*/

        void onCharacteristicChanged(byte[] data);

        void onWriteCharSuccess(String instructions);

        //----------------连接相关--------------start

        void onConnectSuccess(BluetoothDevice device);

        void onConnectFail();

        void onConnectDisconnected();

        //----------------连接相关--------------end

    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容