毕业设计需要,需要用到蓝牙,检测蓝牙强度,当蓝牙强度减弱到一定的程度时,将停止扫描,调用其它函数
下面将实现这样的功能。
环境:Android Studio
官方指南:Ble|Android Developer
1、添加需要的权限
AndroidManifest.xml
<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" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
2、将minSdkVersion api改为18
build.gradle
android {
...
minSdkVersion 18
...
}
3、在onCreate()中添加蓝牙初始化等操作
MainActivity.java
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
//查看是否支持ble
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
finish();
}
bluetoothAdapter = bluetoothManager.getAdapter();
//查看是否支持蓝牙
if (bluetoothAdapter == null ){
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
finish();
}
//如果蓝牙没有打开,打开蓝牙
if(!bluetoothAdapter.isEnabled()){
bluetoothAdapter.enable();
Toast.makeText(this, R.string.turn_on_ble, Toast.LENGTH_SHORT).show();
}
//实例化扫描类
DeviceScanActivity ScanActivity = new DeviceScanActivity();
ScanActivity.bluetoothAdapter = bluetoothAdapter;
//实例化Ble类
ScanActivity.makeBleInstance();
//启动扫描
ScanActivity.scanLeDevice(true);
创建新的java文件DeviceScanActivity.java
DeviceScanActivity.java
package com.zenos.bluetoothtest;
import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Handler;
import java.util.ArrayList;
import java.util.List;
public class DeviceScanActivity extends ListActivity {
public BluetoothAdapter bluetoothAdapter;
protected boolean mScanning;
private Handler handler;
protected int rsiAverage;
protected boolean block =false;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 5000;
private List<Integer> rsiList = new ArrayList<>();
//private BluetoothDevice mBluetoothDevice;
private Ble mBle;
public void scanLeDevice(final boolean enable) {
block = false;
rsiList.clear();
if (enable) {
// Stops scanning after a pre-defined scan period.
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
bluetoothAdapter.stopLeScan(leScanCallback);
rsiAverage = getRsiAverage();
if(rsiAverage < -70){
//信号弱于-70db时,启动block,触发回调函数,启动摄像头拍照。
mBle.doBlock();
}else if(rsiAverage == 0){
System.out.println("=====There is no ble device,please check=====");
}else{
System.out.println("=====scanLeDevice()=====");
scanLeDevice(true);
}
}
}, SCAN_PERIOD);
mScanning = true;
bluetoothAdapter.startLeScan(leScanCallback);
} else {
mScanning = false;
bluetoothAdapter.stopLeScan(leScanCallback);
}
}
private BluetoothAdapter.LeScanCallback leScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi,
byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (device.getAddress().equals("40:06:A0:9C:71:4A")) {
System.out.println("device name: " + device.getName() + ", device address: " + device.getAddress() + ", rssi: " + rssi);
rsiList.add(rssi);
}
}
});
}
};
public int getRsiAverage() {
int sum = 0;
int num = 1;
int avrage = 0;
for (int j = 0; j < rsiList.size(); j++) {
sum = sum + rsiList.get(j);
}
num = rsiList.size();
if(rsiList.size() == 0){
num = 1;
bluetoothAdapter.stopLeScan(leScanCallback);
}
avrage = sum/num;
System.out.println("=====getRsiAverage=====");
System.out.println("avrage: " + avrage);
return avrage;
}
public void makeBleInstance(){
mBle = new Ble();
mBle.setBleListener(new BleListener(){
@Override
public void block() {
System.out.println("a car is in the Parking space!");
System.out.println("Now turn on the camera to shoot!");
}
});
}
//回调函数接口
public interface BleListener{
public void block();
}
public static class Ble{
private BleListener mBleListener;
public void setBleListener(BleListener mBleListener){
this.mBleListener = mBleListener;
}
public void doBlock(){
mBleListener.block();
}
}
}
在rssi小于-70时,将启动启动其它函数
![](https://img.haomeiwen.com/i15518659/04d13061ac491b3a.png)
这里就已经完成rssi的获取
网友评论