正确实施蓝牙发现

时间:2018-05-04 14:34:21

标签: java android bluetooth

我正在尝试为使用蓝牙的Android应用编写代码。我可以访问我已配对的设备,但我无法发现新设备。我看过堆栈溢出,但似乎没有直接给我答案。这是.java.xml及其外观。

public class Discover extends AppCompatActivity {

    private ArrayList<connectableDevices> found = new ArrayList<connectableDevices>(); // appendable name array

    private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // BT adapter

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discover);

        if(mBluetoothAdapter.isDiscovering()){
            mBluetoothAdapter.cancelDiscovery();
        }

        mBluetoothAdapter.startDiscovery();

        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter);

        if (mBluetoothAdapter.isDiscovering()){
            Toast.makeText(this, "Locating nearby devices...", Toast.LENGTH_LONG).show();
        }


        BTAdapter BTadapter = new BTAdapter(this, found);

        ListView deviceList2 = (ListView) findViewById(R.id.discoverList);

        deviceList2.setAdapter(BTadapter);
    }


    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Discovery has found a device. Get the BluetoothDevice
                // object and its info from the Intent.
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String deviceName = device.getName();
                String deviceHardwareAddress = device.getAddress(); // MAC address
                found.add(new connectableDevices(deviceName, deviceHardwareAddress));
                //MAC.add(deviceHardwareAddress);
            }
        }
    };

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

        mBluetoothAdapter.cancelDiscovery();

        // Don't forget to unregister the ACTION_FOUND receiver.
        unregisterReceiver(mReceiver);
    }
}

布局是

<?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.rperdomo.btaccelerometerdata.Discover">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="274dp"
            android:layout_height="44dp"
            android:layout_marginBottom="3dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="3dp"
            android:text="Found Devices"
            android:textAlignment="center"
            android:textColor="#000000"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toTopOf="@+id/discoverList"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ListView
            android:id="@+id/discoverList"
            android:layout_width="395dp"
            android:layout_height="454dp"
            android:layout_marginBottom="40dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent" />
    </android.support.constraint.ConstraintLayout>

应用输出

enter image description here

2 个答案:

答案 0 :(得分:0)

您无法发现新设备,因为设备无法被发现。您需要将您正在寻找的设备置于可发现模式,然后您将在Android应用程序中看到这些设备。请查看几年前我提出的question

但是,如果没有它们处于可发现模式,则无法检测到附近的蓝牙设备。请在问题下方查看我的answer

答案 1 :(得分:0)

需要位置权限,因为蓝牙扫描可用于收集有关用户位置的信息。将这些权限放在AndroidMenifest.xml文件中。

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />