Android 蓝牙4.0开发 实现扫描、连接、通讯、获取通知、特性等 (二、连接蓝牙、通讯、获取通知、特性)

上一篇为:蓝牙连接和扫描

一、新建一个蓝牙服务

因为蓝牙一般连接后可能在应用中的一些地方同步使用 

所以我们可以在连接的类用 单例模式 或者开启服务(我用这个)

public class BleService extends Service{

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public class MyBinder extends Binder {
        public BleService getService() {
            return BleService.this;
        }
    }

}

二、编写蓝牙连接

public void connect(String Address) {
        
        //获取蓝牙适配器
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        //通过蓝牙的物理地址可以获取到蓝牙device 
        mDevice = adapter.getRemoteDevice(mac);
        //连接蓝牙
        mBluetoothGatt = device.connectGatt(this, false, mBluetoothGattCallback);
     //mBluetoothGatt  我们可以用它来获取蓝牙特征值等
    }



//蓝牙回调
 BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {

        // 更改蓝牙 PHY时的回调
        @Override
        public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            super.onPhyUpdate(gatt, txPhy, rxPhy, status);
        }
        //阅读蓝牙 PHY 时的回调
        @Override
        public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            super.onPhyRead(gatt, txPhy, rxPhy, status);
        }

        //蓝牙状态的回调
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)             
        {
            super.onConnectionStateChange(gatt, status, newState);

            //连接成功 
             if (newState == BluetoothGatt.STATE_CONNECTED) {
                //连接成功后  获取到远程设备的特征值、描述符等
                gatt.discoverServices();
            }else {
               
            }

        }
       
        // 读取特征和描述符时的回调  gatt.discoverServices(); status 为读取状态
        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
        }

       //读取蓝牙时的回调
        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
        }
        //发送数据时的回调
        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);
        }
        //通知的回调
        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);
        }

        //读取特征值
        @Override
        public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorRead(gatt, descriptor, status);
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorWrite(gatt, descriptor, status);
        }

        @Override
        public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
            super.onReliableWriteCompleted(gatt, status);
        }

        @Override
        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
            super.onReadRemoteRssi(gatt, rssi, status);
        }

        @Override
        public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
            super.onMtuChanged(gatt, mtu, status);
        }
    }

三、断开蓝牙连接

public void close() {
    if (mBluetoothGatt == null) {
        return;
    }
    mBluetoothGatt.close();
    mBluetoothGatt = null;
}

四、获取特征值

public BluetoothGattCharacteristic getCharacteristic(BluetoothGatt gatt, String uuid) {
        for (BluetoothGattService service : gatt.getServices()) {
            for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
                if (characteristic.getUuid().toString().equals(uuid)) {
                    return characteristic;
                }
            }
        }
        return null;
    }

五、获取描述符

public BluetoothGattDescriptor getDescriptor(BluetoothGattCharacteristic c, String dUuid) {
        if (c != null && dUuid != null) {
            for (BluetoothGattDescriptor descriptor : c.getDescriptors()) {
                if (dUuid.equals(descriptor.getUuid().toString())) {
                    return descriptor;
                }
            }
        }
        return null;
    }

六、发送消息

 public void writeCharacteristic(BluetoothGattCharacteristic characteristic) {
        if (mBluetoothGatt == null) {
            LogUtils.w("BluetoothAdapter not initialized");
            return;
        }
        mBluetoothGatt.writeCharacteristic(characteristic);
    }

七、读取消息

public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
        if (mBluetoothGatt == null) {
            LogUtils.w("BluetoothAdapter not initialized");
            return;
        }
        mBluetoothGatt.readCharacteristic(characteristic);
    }

 

八、读取通知

 public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, BluetoothGattDescriptor descriptor, boolean enabled) {
        if (mBluetoothGatt == null) {
            LogUtils.w("BluetoothAdapter not initialized");
            return;
        }
        LogUtils.d("获取通知");
        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        mBluetoothGatt.writeDescriptor(descriptor);
    }

九、使用

1、可以在需要蓝牙前启动蓝牙服务

 Intent intent = new Intent(this, BleService.class);
 startService(intent);

//没有用时记得关闭哦

stopService(intent);

2、在需要用到蓝牙的地方绑定蓝牙服务

private ServiceConnection mConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //获取到蓝牙服务
            mBleService = ((BleService.MyBinder) service).getService();
          
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }; 

@Override
 public void create() {
     Intent intent = new Intent(mView, BleService.class);
     bindService(intent, mConn, Service.BIND_AUTO_CREATE);
}

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值