Android低功耗蓝牙BLE

本文详细介绍低功耗蓝牙(BLE)的开发流程,包括权限配置、RSSI获取、搜索与连接设备、服务及特征值交互等内容,并提供关键代码示例。

        低功耗蓝牙BLE与传统的蓝牙相比最大的优势是功耗降低90%,同时传输距离增大(超过100米)、安全和稳定性提高(支持AES加密和CRC验证),允许Android应用程序与具有更严格电源要求的BLE设备进行通信,如接近传感器、心率传感器等低功耗设备。但是BLE蓝牙一包数据最多20个字节,因此在Android系统下传输大量数据就不太合适,还的需要使用经典蓝牙。

一、蓝牙权限

1、申请权限

静态注册:

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

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

动态申请定位权限:

String[] p = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
ActivityCompat.requestPermissions(activity,p,1000);

如果您的目标是 Android 10,那么您需要 ACCESS_FINE_LOCATION 来扫描。ACCESS_COARSE_LOCATION 在 Android 10 中不再起作用。本次项目targetApi为29。

2、打开手机定位

Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
MainActivity a = (MainActivity) activity;
a.startActivityForResult(intent, OPEN_GPS_CODE)

注意:蓝牙的搜索、配对等等后续功能都需要开启权限,否则啥也获取不到。

二、获取蓝牙设备的RSSI

1、已经配对

BluetoothDevice remoteDevice = bluetoothAdapter.getRemoteDevice(deviceId);
remoteDevice.connectGatt(activity, false, new BluetoothGattCallback() {

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        super.onConnectionStateChange(gatt, status, newState);
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    gatt.readRemoteRssi();
                }
            }, 1000);
    }

    @Override
    public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
        super.onReadRemoteRssi(gatt, rssi, status);
        LogUtils.d("onReadRemoteRssi:" + rssi);
    }
});

2、未配对

public void getBLEDeviceRSSI(JsBLEBean bean) {
    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    activity.registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)){
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (device.getAddress().equals(deviceId)){
                    short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, (short) 0);
                    //信号强度
                    LogUtils.d("device.getAddress():"+device.getAddress());
                    LogUtils.d("device.getAddress():"+rssi);
                }
            }
        }
    }, filter);
    bluetoothAdapter.startDiscovery();
}

三、BLE蓝牙搜索

已知蓝牙的Mac地址:deviceId

//过滤条件
ArrayList<ScanFilter> scanFilterList = new ArrayList<>();
ScanFilter scanFilter = new ScanFilter.Builder()
        .setDeviceAddress(deviceId)
        .build();
scanFilterList.add(scanFilter);
//扫描设置。
ScanSettings scanSettings = new ScanSettings.Builder()
        .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
        .setReportDelay(0)
        .build();
//连接
BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
BleScanning bleScanning = new BleScanning(callback);
bluetoothLeScanner.startScan(scanFilterList, scanSettings, new ScanCallback() {
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        super.onScanResult(callbackType, result);
    }

    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        super.onBatchScanResults(results);
    }

    @Override
    public void onScanFailed(int errorCode) {
        super.onScanFailed(errorCode);
    }
});
评论 6
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值