我的Java代码无法搜索附近的蓝牙设备

时间:2019-07-19 09:06:41

标签: java android bluetooth bluetoothadapter

我写了一个代码以列出附近的蓝牙设备,但是我的代码无法检测到任何蓝牙设备。 根据我的代码,当我执行String action = intent.getAction();时,该操作只是开始和结束而不会在日志中打印任何设备。 Log.i(“ Action”,action);  在BroadCastReciever下的onRecieve()中。

这是我的MainActivity.java:

package com.example.findbluetoothdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    TextView statusTextView;
    ListView listView;
    Button searchButton;
    ArrayList<String> bluetoothDevices = new ArrayList<>();
    ArrayList<String> addresses = new ArrayList<>();
    ArrayAdapter arrayAdapter;

    BluetoothAdapter bluetoothAdapter;

    private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.i("Action",action);

            if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                statusTextView.setText("Finished");
                searchButton.setEnabled(true);
            } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String name = device.getName();
                String address = device.getAddress();
                String rssi = Integer.toString(intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE));
                Log.i("Device Found","Nmae :"+name+" Address :"+address+" RSSI :"+rssi);

                if (!addresses.contains(address)) {
                    addresses.add(address);
                    String deviceString = "";
                    if (name==null || name.equals("")) {

                        deviceString  = address+" - RSSI "+rssi+"dBm";
                    } else {
                        deviceString = name+" - RSSI "+rssi+"dBm";
                    }
                        bluetoothDevices.add(deviceString);

                    arrayAdapter.notifyDataSetChanged();
                }

            }
        }
    };

    public void searchFunction(View view) {
        bluetoothDevices.add("ADDING NEW DEVICES...");
        statusTextView.setText("Searching...");
        searchButton.setEnabled(false);

        arrayAdapter.notifyDataSetChanged();
        bluetoothDevices.clear();
        addresses.clear();
        bluetoothAdapter.startDiscovery();




    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        statusTextView = findViewById(R.id.statusTextView);
        listView = findViewById(R.id.listView);
        searchButton = findViewById(R.id.button);

        arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,bluetoothDevices);
        listView.setAdapter(arrayAdapter);


        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(broadcastReceiver ,intentFilter);

    }
}

这是AndroidManifest.xml:

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

    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>
    <uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
    <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/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

1 个答案:

答案 0 :(得分:0)

如果尚未完成操作,则需要征求许可。

int premissionCehck = context.checkSelfPermission("Manifest.premission.ACCES_FINE_LOCATION");
premissionCehck += context.checkSelfPermission("Manifest.premission.ACCES_FINE_LOCATION");
if (premissionCehck != 0) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001);
                    }

请注意,如果您已经拒绝了一次权限,则权限请求不会再出现,因此您需要在android设置中免费提供该权限。