Android BLE错误:D/BluetoothLeScanner:扫描失败,原因:应用程序注册失败

Android BLE错误:D/BluetoothLeScanner:扫描失败,原因:应用程序注册失败,android,bluetooth-lowenergy,bluetooth-gatt,Android,Bluetooth Lowenergy,Bluetooth Gatt,我在android studio中尝试扫描BLE时有时会出现此错误,通常是在使用我的应用程序一段时间后,但并非总是如此。 我正在使用一个专门用于BT连接的类,这是其中的内容: package com.omri.goniometer; import android.app.Application; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.

我在android studio中尝试扫描BLE时有时会出现此错误,通常是在使用我的应用程序一段时间后,但并非总是如此。 我正在使用一个专门用于BT连接的类,这是其中的内容:

package com.omri.goniometer;

import android.app.Application;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.HandlerThread;
import android.util.Log;
import android.widget.Toast;
import android.os.Handler;

import java.util.List;
import java.util.UUID;


public class Bluetooth {
    private final Context mContext;
    private final MainActivity mActivity;
    final String devcAddress = "D8:A0:1D:47:49:82";
//    private static final UUID ServiceUUID = UUID.fromString("0000183B-0000-1000-8000-00805F9B34FB");
//    private static final UUID CharUUID = UUID.fromString("00002A08-0000-1000-8000-00805F9B34FB");
    private static final UUID ServiceUUID = UUID.fromString("da3a95de-467b-11ea-b77f-2e728ce88125");
    private static final UUID CharUUID = UUID.fromString("da3a9836-467b-11ea-b77f-2e728ce88125");
    private String scanDevcAddress = null;
    private BluetoothAdapter mBTAdapter;
    private boolean mScanning;
    private static final long SCAN_PERIOD = 5000;
    private BluetoothLeScanner mBluetoothLeScanner;
    private BluetoothDevice BTdevice;
    private BluetoothGatt mBTGatt;
    private BluetoothGattCharacteristic mGattChar;
    private int mConnectionState = STATE_DISCONNECTED;
    private Short rollInt = 0;
    private Short pitchInt = 0;
    String rollString = "0";
    String pitchString = "0";
    private static final int STATE_DISCONNECTED = 0;
    private static final int STATE_CONNECTING = 1;
    private static final int STATE_CONNECTED = 2;
    private static final int WAVE = 3;
    private static final int BATTERY_UPDATE = 4;
    String[] stringArray;
    Handler mHandler;
    public final static String ACTION_GATT_CONNECTED =
            "com.example.bluetooth.le.ACTION_GATT_CONNECTED";
    public final static String ACTION_GATT_DISCONNECTED =
            "com.example.bluetooth.le.ACTION_GATT_DISCONNECTED";
    public final static String ACTION_GATT_SERVICES_DISCOVERED =
            "com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED";
    public final static String ACTION_DATA_AVAILABLE =
            "com.example.bluetooth.le.ACTION_DATA_AVAILABLE";
    public final static String EXTRA_DATA =
            "com.example.bluetooth.le.EXTRA_DATA";

    Bluetooth(Context context, MainActivity activity) {
        this.mContext = context;
        this.mActivity = activity;
        mHandler = new Handler();
        disconnect();
        close();

        // Initializes Bluetooth adapter.
        final BluetoothManager bluetoothManager =
                (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
        mBTAdapter = bluetoothManager.getAdapter();
        mBluetoothLeScanner = mBTAdapter.getBluetoothLeScanner();
        if (mBTAdapter == null) {
            // Device does not support Bluetooth
            Toast.makeText(mContext, "Bluetooth device not found!", Toast.LENGTH_SHORT).show();
        } else {

            if (!mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
                Toast.makeText(mContext, "no BLE feature in phone", Toast.LENGTH_SHORT).show();
            }

            searchDevice();

            //connect();

        }

    }

    public void searchDevice() {
        if (mBTAdapter.isEnabled()) {
            Log.d("ADebugTag", "After check if BTAdapter is enabled");
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    scanLeDevice(true);
                }
            }, 5000);
        } else {
            Toast.makeText(mContext, "To begin please turn on bluetooth", Toast.LENGTH_SHORT).show();
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            mActivity.startActivityForResult(enableBtIntent, MainActivity.REQUEST_ENABLE_BT);
        }
    }

    private void scanLeDevice(final boolean enable) {
        if (enable) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mScanning = false;
                    mBluetoothLeScanner.stopScan(leScanCallback);
                }
            }, SCAN_PERIOD);
            mScanning = true;
            Log.d("ADebugTag", "Just before scan");
            mBluetoothLeScanner.startScan(leScanCallback);
        } else {
            mScanning = false;
            mBluetoothLeScanner.stopScan(leScanCallback);
        }
    }

    // Device scan callback.
    private ScanCallback leScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            Log.d("ADebugTag", "Inside scan callback scan");
            BTdevice = result.getDevice();
            scanDevcAddress = result.getDevice().getAddress();
            Log.d("ADebugTag", "scanned address: " + scanDevcAddress);
            if (scanDevcAddress.equals(devcAddress)) {
                mActivity.updatesStatus(MainActivity.DEVICE_FOUND,"","");
                //Intent deviceFoundIntent = new Intent(mActivity,MainActivity.class);
                scanLeDevice(false);

                //Toast.makeText(mContext, "Scanning stopped", Toast.LENGTH_SHORT).show();
            }
        }
    };

    public boolean connect() {
        mActivity.updatesStatus(MainActivity.STATE_CONNECTING,"","");
        BTdevice = mBTAdapter.getRemoteDevice(devcAddress);
        mBTGatt = BTdevice.connectGatt(mContext, false, mGattCallback);
        if (mBTGatt == null){
            Log.d("ADebugTag", "mBTGatt is null");
            return false;
        }
        else {
            try {
                mGattChar = mBTGatt.getService(ServiceUUID).getCharacteristic(CharUUID);
            }
            catch (NullPointerException e){
                Log.d("ADebugTag", "mGattChar is null\n" + e);
            }
            //setCharacteristicNotification(mGattChar, true);
            return true;
        }
    }

    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            String intentAction;
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                intentAction = ACTION_GATT_CONNECTED;
                mConnectionState = STATE_CONNECTED;
                broadcastUpdate(intentAction);
                mActivity.updatesStatus(MainActivity.STATE_CONNECTED,"","");
                Log.d("ADebugTag", "Connected to GATT server.");
                // Attempts to discover services after successful connection.
                Log.d("ADebugTag", "Attempting to start service discovery:" + mBTGatt.discoverServices());
            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                intentAction = ACTION_GATT_DISCONNECTED;
                mConnectionState = STATE_DISCONNECTED;
                mActivity.updatesStatus(MainActivity.STATE_DISCONNECTED,"","");
                Log.d("ADebugTag", "Disconnected from GATT server.");
                broadcastUpdate(intentAction);
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
                if (mBTGatt ==null){
                    return;
                }
                else {
                    List<BluetoothGattService> services = getSupportedGattServices();
                    for (BluetoothGattService service : services) {
                        if (!service.getUuid().equals(ServiceUUID))
                            continue;

                        List<BluetoothGattCharacteristic> gattCharacteristics =
                                service.getCharacteristics();

                        // Loops through available Characteristics.
                        for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                            if (!gattCharacteristic.getUuid().equals(CharUUID))
                                continue;

                            final int charaProp = gattCharacteristic.getProperties();

                            if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                                setCharacteristicNotification(gattCharacteristic, true);
                            } else {
                                Log.d("ADebugTag", "Characteristic does not support notify");
                            }
                        }
                    }
                }
            } else {
                Log.d("ADebugTag", "onServicesDiscovered received:" + status);
            }
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt,
                                         BluetoothGattCharacteristic characteristic,
                                         int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
                final byte[] dataInput = characteristic.getValue();
            }
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt,
                                            BluetoothGattCharacteristic characteristic) {
            final byte[] dataInput = characteristic.getValue();
            //Log.d("ADebugTag", "Data on changed: " + dataInput);
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }
    };

    private void broadcastUpdate(final String action) {
        final Intent intent = new Intent(action);
        mContext.sendBroadcast(intent);
    }

    private void broadcastUpdate(final String action,
                                 final BluetoothGattCharacteristic characteristic) {
        final Intent intent = new Intent(action);
        // For all other profiles, writes the data formatted in HEX.
        if (characteristic == null) {
            Log.d("ADebugTag", "characteristic is null in broadcastUpdate");
        }
        else {
            final byte[] data = characteristic.getValue();
            //Log.d("ADebugTag", "Data broadcast update: " + data);
            if (data != null && data.length > 0) {
                final StringBuilder stringBuilder = new StringBuilder(data.length);
                for (byte byteChar : data)
                    stringBuilder.append(String.format("%X ", byteChar));
                //stringBuilder.append(String.format("%d ", byteChar));
                String ds = stringBuilder.toString();
                intent.putExtra(EXTRA_DATA, new String(data) + "\n" + ds);
                Log.d("ADebugTag", "Data complete string: " + ds);
                    stringArray = ds.split(" ");
                    rollInt = (short) Integer.parseInt(stringArray[3]+stringArray[2],16);
                    pitchInt = (short) Integer.parseInt(stringArray[1]+stringArray[0],16);
                    Log.d("ADebugTag", "Roll received:" + rollInt);
                    Log.d("ADebugTag", "Pitch received:" + pitchInt);
                    rollString = rollInt.toString();
                    pitchString = pitchInt.toString();
                    mActivity.updatesStatus(MainActivity.DATA_RECEIVED,rollString,pitchString);
                    //deciBattery = Integer.parseInt(bateryStringArray[0],16);
                    //updatesStatus(BATTERY_UPDATE);
            }
            mContext.sendBroadcast(intent);
        }
    }

    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                              boolean enabled) {
        if (mBTAdapter == null || mBTGatt == null) {
            Log.d("ADebugTag", "BluetoothAdapter not initialized in set char");
            return;
        }
        else {
            //mGattChar = mBTGatt.getService()
            mBTGatt.setCharacteristicNotification(characteristic, enabled);
//            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CharUUID);
//            descriptor.setValue(descriptor.ENABLE_NOTIFICATION_VALUE);
//            mBTGatt.writeDescriptor(descriptor);
        }
    }

    /**
     * Disconnects an existing connection or cancel a pending connection. The disconnection result
     * is reported asynchronously through the
     * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
     * callback.
     */
    public void disconnect() {
        if (mBTAdapter == null || mBTGatt == null) {
            Log.d("ADebugTag", "BluetoothAdapter not initialized");
            return;
        }
        mBTGatt.disconnect();
    }
    public List<BluetoothGattService> getSupportedGattServices() {
        if (mBTGatt == null) return null;

        return mBTGatt.getServices();
    }
    /**
     * After using a given BLE device, the app must call this method to ensure resources are
     * released properly.
     */
    public void close() {
        if (mBTGatt == null) {
            return;
        }
        mBTGatt.close();
        mBTGatt = null;
    }

}

组件com.omri.goniometer;
导入android.app.Application;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.bluetooth.BluetoothGatt;
导入android.bluetooth.BluetoothGattCallback;
导入android.bluetooth.BluetoothGattCharacteristic;
导入android.bluetooth.BluetoothGattService;
导入android.bluetooth.BluetoothManager;
导入android.bluetooth.le.BluetoothLeScanner;
导入android.bluetooth.BluetoothProfile;
导入android.bluetooth.le.ScanCallback;
导入android.bluetooth.le.ScanResult;
导入android.content.Context;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.os.HandlerThread;
导入android.util.Log;
导入android.widget.Toast;
导入android.os.Handler;
导入java.util.List;
导入java.util.UUID;
公共级蓝牙{
私有最终上下文mContext;
私人最终维护活动;
最终字符串devcAddress=“D8:A0:1D:47:49:82”;
//专用静态最终UUID服务UUID=UUID.fromString(“0000183B-0000-1000-8000-00805F9B34FB”);
//私有静态最终UUID CharUUID=UUID.fromString(“00002A08-0000-1000-8000-00805F9B34FB”);
专用静态最终UUID服务UUID=UUID.fromString(“da3a95de-467b-11ea-b77f-2e728ce88125”);
专用静态最终UUID CharUUID=UUID.fromString(“da3a9836-467b-11ea-b77f-2e728ce88125”);
私有字符串ScandevCadAddress=null;
私人蓝牙适配器;
私有布尔扫描;
专用静态最终长扫描周期=5000;
私人BluetoothLeScanner mBluetoothLeScanner;
私人蓝牙设备;
私人蓝牙总协定;
私有BluetoothGattCharacteristic mGattChar;
private int mConnectionState=状态_已断开连接;
私有短rollInt=0;
私有短pitchInt=0;
String rollString=“0”;
字符串pitchString=“0”;
私有静态最终int状态_DISCONNECTED=0;
私有静态最终int状态_CONNECTING=1;
专用静态最终int状态_CONNECTED=2;
专用静态最终int波=3;
专用静态最终整备电池\u更新=4;
字符串[]字符串数组;
汉德勒;
公共最终静态字符串动作\u GATT\u已连接=
“com.example.bluetooth.le.ACTION\u GATT\u CONNECTED”;
公共最终静态字符串操作\u GATT\u断开连接=
“com.example.bluetooth.le.ACTION\u GATT\u DISCONNECTED”;
公共最终静态字符串操作\u GATT\u服务\u发现=
“com.example.bluetooth.le.ACTION\u GATT\u SERVICES\u DISCOVERED”;
公共最终静态字符串操作\u数据\u可用=
“com.example.bluetooth.le.ACTION\u数据可用”;
公共最终静态字符串额外_数据=
“com.example.bluetooth.le.EXTRA_DATA”;
蓝牙(上下文,主活动){
this.mContext=上下文;
这个。活动=活动;
mHandler=新处理程序();
断开连接();
close();
//初始化蓝牙适配器。
最终BluetoothManager BluetoothManager=
(BluetoothManager)mContext.getSystemService(Context.BLUETOOTH\u服务);
mBTAdapter=bluetoothManager.getAdapter();
mBluetoothLeScanner=mBTAdapter.getBluetoothLeScanner();
if(mBTAdapter==null){
//设备不支持蓝牙
Toast.makeText(mContext,“找不到蓝牙设备!”,Toast.LENGTH_SHORT.show();
}否则{
如果(!mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE\u BLUETOOTH\u LE)){
Toast.makeText(mContext,“手机中没有BLE功能”,Toast.LENGTH_SHORT.show();
}
搜索设备();
//connect();
}
}
公共设备(){
if(mBTAdapter.isEnabled()){
Log.d(“ADebugTag”,“检查BTAdapter是否启用后”);
mHandler.postDelayed(新的Runnable(){
@凌驾
公开募捐{
扫描设备(真实);
}
}, 5000);
}否则{
Toast.makeText(mContext,“要开始,请打开蓝牙”,Toast.LENGTH_SHORT.show();
Intent enablebintent=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
mActivity.startActivityForResult(enableBtIntent,MainActivity.REQUEST\u ENABLE\u BT);
}
}
专用void扫描设备(最终布尔启用){
如果(启用){
mHandler.postDelayed(新的Runnable(){
@凌驾
公开募捐{
mScanning=false;
mBluetoothLeScanner.stopScan(leScanCallback);
}
},扫描周期);
mScanning=true;
Log.d(“ADebugTag”,“扫描前”);
mbluetotherlescanner.startScan(lescallback);
}否则{
mScanning=false;
mBluetoothLeScanner.stopScan(leScanCallback);
}
}
//设备扫描回调。
private ScanCallback leScanCallback=新建ScanCallback(){
@凌驾
公共void onScanResult(int callbackType、ScanResult){
Log.d(“ADebugTag”,“内部扫描回调扫描”);
BTdevice=result.getDevice();
ScandevCadAddress=result.getDevice().getAddress();
日志d(“ADebugTag”,“扫描地址:“+scanDevcAddress”);
if(scanDevcAddress.equals(devcAddress)){
mActivity.updateStatus(找到MainActivity.DEVICE“,”);
//意向设备意向=新意向(MacActivity,MainActivity.class);
扫描设备(假);