有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java找不到可扩展设备

我是安卓应用程序的新手。我制作了一个BLE扫描仪来检测BLE设备。我已经使用stopLeScan和StarteScan函数根据我所遵循的教程对其进行了编码。然而,我无法检测到任何不可靠的设备。在编写代码时,我发现这些函数现在已经过时了。是因为我使用了过时的函数吗?或者我的代码中还有其他错误?如果是过时的功能,你能告诉我如何修改它,使其正常工作吗? 仅供参考:我正在安卓 pie上运行它

我已经看过安卓开发者的例子,不同的stackoverflow解决方案。我遵循的教程如下所述: https://github.com/kaviles/BLE_Tutorials/tree/master/Android/01

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    package="com.example.ble_scanner">

    <uses-permission 安卓:name="安卓.permission.BLUETOOTH"/>
    <uses-permission 安卓:name="安卓.permission.BLUETOOTH_ADMIN"/>
    <uses-permission 安卓:name="安卓.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission 安卓:name="安卓.permission.ACCESS_COARSE_LOCATION"/>
    <uses-feature 安卓:name="安卓.hardware.bluetooth_le" 安卓:required="true"/>

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

                <category 安卓:name="安卓.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是主要的活动:

public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private final static String TAG = MainActivity.class.getSimpleName();

    public static final int REQUEST_ENABLE_BT = 1;

    private HashMap<String, BTLE_Device> mBTDevicesHashMap;
    private ArrayList<BTLE_Device> mBTDevicesArrayList;
    private ListAdapter_BTLE_Devices adapter;

    private Button btn_Scan;

    private BroadcastReceiver_BTState mBTStateUpdateReceiver;
    private Scanner_BTLE mBTLeScanner;

    private static final int LOCATION_REQUEST = 255;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)){
            Utils.toast(getApplicationContext(),"BLE not supported");
            finish();
        }


        mBTStateUpdateReceiver = new BroadcastReceiver_BTState(getApplicationContext());
        mBTLeScanner = new Scanner_BTLE(this,15000,-150);

        mBTDevicesHashMap = new HashMap<>();
        mBTDevicesArrayList = new ArrayList<>();

        adapter = new ListAdapter_BTLE_Devices(this, R.layout.btle_device_list_item, mBTDevicesArrayList);

        ListView listView = new ListView(this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);

        btn_Scan = (Button) findViewById(R.id.btn_scan);
        ((ScrollView)findViewById(R.id.device_list)).addView(listView);
        findViewById(R.id.btn_scan).setOnClickListener(this);
    }
    @Override
    protected void onStart(){
        super.onStart();

        registerReceiver(mBTStateUpdateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
    }

    @Override
    protected void onResume(){
        super.onResume();
    }

    @Override
    protected void onPause(){
        super.onPause();
        stopScan();
    }

    protected void onStop(){
        super.onStop();
        unregisterReceiver(mBTStateUpdateReceiver);
        stopScan();
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){

        if (requestCode == REQUEST_ENABLE_BT){
            if (resultCode == RESULT_OK){
                Utils.toast(getApplicationContext(),"Bluetooth is ready to use");
            }
            else if (resultCode == RESULT_CANCELED){
                Utils.toast(getApplicationContext(),"Please turn on Bluetooth");
            }
        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        Context context = view.getContext();
//        stopScan();
//
//        String name = mBTDevicesArrayList.get(position).getName();
//        String address = mBTDevicesArrayList.get(position).getAddress();

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_scan:
                Utils.toast(getApplicationContext(),"Scan button pressed");

                if(!mBTLeScanner.isScanning()){
                    startScan();
                } else {
                    stopScan();
                }
                break;
            default:
                break;
        }

    }

    public void addDevice(BluetoothDevice device, int rssi) {

        String address = device.getAddress();
        if(!mBTDevicesHashMap.containsKey(address)){
            BTLE_Device btle_device = new BTLE_Device(device);
            btle_device.setRssi(rssi);

            mBTDevicesHashMap.put(address, btle_device);
            mBTDevicesArrayList.add(btle_device);
        } else {
            mBTDevicesHashMap.get(address).setRssi(rssi);
        }
        adapter.notifyDataSetChanged();

    }

    public void startScan(){
        btn_Scan.setText("Scanning...");

        mBTDevicesArrayList.clear();
        mBTDevicesHashMap.clear();

        adapter.notifyDataSetChanged();
        mBTLeScanner.start();
    }

    public void stopScan() {
        btn_Scan.setText("Scan Again");

        mBTLeScanner.stop();
    }

    @TargetApi(23)
    private void verifyPermissionAndScan() {
        if (ContextCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION) == PERMISSION_GRANTED) {
            startScan();
        } else {
            requestPermissions(new String[] {ACCESS_COARSE_LOCATION}, LOCATION_REQUEST);
        }
    }

    @Override
    public void onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode != LOCATION_REQUEST) return;

        if (grantResults.length > 0 && grantResults[0] == PERMISSION_GRANTED) {
            startScan();
        } else {
            Toast.makeText(this, R.string.location_permission_toast, Toast.LENGTH_LONG).show();
        }
    }

}

这是带有适配器的java类文件:

public class Scanner_BTLE {

    private MainActivity ma;

    private BluetoothAdapter mBluetoothAdapter;
    private boolean mScanning;
    private Handler mHandler;

    private long scanPeriod;
    private int signalStrength;

    public Scanner_BTLE(MainActivity mainActivity, long scanPeriod, int signalStrength) {
        ma = mainActivity;

        mHandler = new Handler();

        this.scanPeriod = scanPeriod;
        this.signalStrength = signalStrength;

        final BluetoothManager bluetoothManager = (BluetoothManager) ma.getSystemService(Context.BLUETOOTH_SERVICE);

        mBluetoothAdapter = bluetoothManager.getAdapter();
    }

    public boolean isScanning() {
        return mScanning;
    }

    public void start() {
        if (!Utils.checkBluetooth(mBluetoothAdapter)) {
            Utils.requestUserBluetooth(ma);
            ma.stopScan();
        } else {
            scanLeDevice(true);
        }
    }

    public void stop() {
        scanLeDevice(false);
    }

    private void scanLeDevice(final boolean enable) {
        if (enable && !mScanning) {
            Utils.toast(ma.getApplication(), "Starting BLE scan...");

            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Utils.toast(ma.getApplicationContext(), "Stopping BLE scan...");

                    mScanning = false;
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);

                    ma.stopScan();
                }
            }, scanPeriod);
            mScanning = true;
            mBluetoothAdapter.startLeScan(mLeScanCallback);
        } else {
            mScanning = false;
            mBluetoothAdapter.stopLeScan(mLeScanCallback);
        }
    }

    private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {
                @Override
                public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {

                    final int new_rssi = rssi;
                    if (rssi > signalStrength) {
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                ma.addDevice(device, new_rssi);
                            }
                        });
                    }
                }
            };

}

其余文件如本教程所示

以下是我的主要XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:paddingTop="20dp"
    安卓:paddingBottom="20dp"
    安卓:paddingLeft="20dp"
    安卓:paddingRight="20dp"
    安卓:orientation="vertical"
    tools:context=".MainActivity">

    <ScrollView
        安卓:id="@+id/device_list"
        安卓:layout_width="match_parent"
        安卓:layout_height="515dp"
        安卓:fillViewport="true">

    </ScrollView>

    <Button
        安卓:id="@+id/btn_scan"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:text="@string/start_scan" />

</LinearLayout>

这是我的魔鬼信息XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="match_parent" 安卓:layout_height="match_parent">

    <LinearLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:orientation="vertical">
        <LinearLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="0dp"
            安卓:layout_weight="2"
            安卓:orientation="horizontal">
            <TextView
                安卓:layout_width="0dp"
                安卓:layout_height="wrap_content"
                安卓:layout_weight="4"
                安卓:textColor="@安卓:color/black"
                安卓:id="@+id/tv_name"/>

            <TextView
                安卓:layout_width="0dp"
                安卓:layout_height="wrap_content"
                安卓:layout_weight="1"
                安卓:textColor="@安卓:color/black" 安卓:id="@+id/tv_rssi"/>

        </LinearLayout>

        <LinearLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="0dp"
            安卓:layout_weight="1"
            安卓:orientation="horizontal">

            <TextView
                安卓:layout_width="0dp"
                安卓:layout_height="wrap_content"
                安卓:layout_weight="1"
                安卓:textColor="@安卓:color/black"
                安卓:id="@+id/tv_macaddr"/>

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

我没有看到任何错误消息。所以我有点困惑该做什么,我做错了什么


共 (0) 个答案