微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

即使我的 Android 看到它们,设备扫描也不会返回可用的 BLE 设备

如何解决即使我的 Android 看到它们,设备扫描也不会返回可用的 BLE 设备

我可以理解,非 BLE 设备不会使用 Startdiscovery 响应 BLE 客户端设备 (Android),但应该响应扫描请求。我相信 AdaFruit 正在响应发现事件,因为我正在通过设置/连接/蓝牙显示中的 Android 设备应用程序获得对范围内设备的发现响应。这包括 Adafruit BLE 设备。同样,我相信这意味着问题出在 Android 方面。我已经搜索了所有我能找到的东西,但我得到的大部分信息都是几年前的。我专注于称为 ScanStart 和 LeScanCallbac 的潜在问题。我什至不知道我的回调是否被触发。任何帮助将不胜感激!

enter code here

My Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mjhol.discoverdevices1" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.discoverdevices1" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


    </application>

    <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-feature android:name="android.hardware.bluetooth_le" android:required="true">
    </uses-feature>
</manifest>


enter code here

包 com.mjhol.discoverdevices1;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.broadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;


@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class MainActivity extends AppCompatActivity {

// Setup global variables here
private boolean mScanning = false;
private Handler handler = new Handler();
private static final long SCAN_PERIOD = 10000;   // Scan for 10 sec max
private BluetoothLeScanner btLeScanner =
        BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();
private ScanCallback leDevicelistadapter;

@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    int REQUEST_ENABLE_BT = 44;

// Setup local variables here
    Toast.makeText(getApplicationContext(),"Entered discoverDevices",Toast.LENGTH_SHORT).show();

    Button DL_btnClick = (Button) findViewById(R.id.DD_btn);
    Button DS_btnClick = (Button) findViewById(R.id.DS_btn);

    final BluetoothManager bluetoothManager =
            (BluetoothManager) getSystemService(Context.BLUetoOTH_SERVICE);
    final BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

    // Bluetooth eabled?
    if (btAdapter == null || !btAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);
    }

    if (btAdapter != null)
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) !=
                PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[] {Manifest.permission.ACCESS_FINE_LOCATION},123);
        }

    Toast.makeText(getApplicationContext(),"Start BLE scan",Toast.LENGTH_SHORT).show();

    // This starts BLE Scan
    DS_btnClick.setonClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(),"Entered BLE Scan code "+mScanning,Toast.LENGTH_SHORT).show();
            if (!mScanning) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mScanning = false;
                        Toast.makeText(getApplicationContext(),"At 1",Toast.LENGTH_SHORT).show();
                        btLeScanner.stopScan((ScanCallback) scanCallback);
                    }
                },SCAN_PERIOD);

                Toast.makeText(getApplicationContext(),"At 2",Toast.LENGTH_SHORT).show();
                mScanning = true;
                Toast.makeText(getApplicationContext(),"At 3",Toast.LENGTH_SHORT).show();
                btLeScanner.startScan((ScanCallback) scanCallback);
                Toast.makeText(getApplicationContext(),"At 4",Toast.LENGTH_SHORT).show();

            } else {
                mScanning = false;
                Toast.makeText(getApplicationContext(),"At 5",Toast.LENGTH_SHORT).show();
                btLeScanner.stopScan((ScanCallback) scanCallback);
            }
        }
    });
}


private void requestPermissions(String permissionName,int permissionRequestCode) {
    Toast.makeText(getApplicationContext(),"RequestingPermission",Toast.LENGTH_SHORT).show();
    Toast.makeText(getApplicationContext(),permissionName,Toast.LENGTH_SHORT).show();
    ActivityCompat.requestPermissions(this,new String[]{permissionName},permissionRequestCode);
};

    private BluetoothAdapter.LeScanCallback scanCallback =
            new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(BluetoothDevice device,int RSSi,byte[] scanRecord) {
            Toast.makeText(getApplicationContext(),"Got ScanCallback",Toast.LENGTH_SHORT).show();
        }
    };

 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。