android studio 低功耗蓝牙ble ctrlCV即用版

(这个版本是仅支持低功耗蓝牙协议,测试用模块为正点原子atk-ble02)

1.添加权限,在manifest.xml中添加

    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

2.创建类Ble,ctrlCV:

public class Ble {
    UUID uuid_ser = UUID.fromString("9ecadc24-0ee5-a9e0-93f3-a3b50100406e");
    UUID uuid_ch_wt = UUID.fromString("9ecadc24-0ee5-a9e0-93f3-a3b50200406e");
    UUID uuid_ch_rd = UUID.fromString("9ecadc24-0ee5-a9e0-93f3-a3b50300406e");
    UUID uuid_descriptor = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    BluetoothDevice mdevice;
    BluetoothGatt gatt;
    BluetoothGattCharacteristic characteristic_write;
    BluetoothGattCharacteristic characteristic_read;
    BluetoothGattDescriptor descriptor;
    Handler handler = new Handler();
    private String get_str = "null";

    BluetoothGattCallback callback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            if(newState == BluetoothProfile.STATE_CONNECTED){
                gatt.discoverServices();
                Log.e("--","connect success");
            }
            else{
                Log.e("--","connect failed");
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            List<BluetoothGattService> list_ser = gatt.getServices();
            for(BluetoothGattService service : list_ser){
                Log.e("ser--",service.getUuid().toString());
                List<BluetoothGattCharacteristic> list_ch = service.getCharacteristics();
                for(BluetoothGattCharacteristic characteristic : list_ch){
                    Log.e("chara-----",characteristic.getUuid().toString());
                    if(uuid_ch_rd.equals(characteristic.getUuid())){
                        characteristic_read = characteristic;
                        Log.e("--","found rd!");
                    }
                    if(uuid_ch_wt.equals(characteristic.getUuid())){
                        characteristic_write = characteristic;
                        Log.e("--","found wt!");
                    }
                }
            }
            try{
                gatt.setCharacteristicNotification(characteristic_read, true);
                descriptor = characteristic_read.getDescriptor(uuid_descriptor);
                descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                gatt.writeDescriptor(descriptor);
            }
            catch (SecurityException e){
                Log.e("--","error");
            }
        }
        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            Log.d("--","成功写入");
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Log.e("--","连接成功");
                }
            });
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            Log.e("!!!","changed!");
            byte[] data = characteristic.getValue();
            Log.e("!!!",new String(data));
            get_str = new String(data);
        }
    };
    public void connect(Context context, String device_name){
        for(BluetoothDevice device : adapter.getBondedDevices()){
            if(device_name.equals(device.getName())){
                mdevice = device;
            }
            break;
        }
        gatt = mdevice.connectGatt(context, false, callback);
        gatt.connect();
    }
    public void send_msg(String msg){
        characteristic_write.setValue(msg);
        gatt.writeCharacteristic(characteristic_write);
    }
    public String get_msg(){
        return get_str;
    }
}

3.直接使用方法即可

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值