首页 > 解决方案 > BroadcastReceveir 不适用于蓝牙

问题描述

我尝试使用蓝牙在 android 中创建简单的应用程序。我在查找设备时遇到问题。

我有相同的代码,就像在教程中一样:https ://www.youtube.com/watch?v=hv_-tX1VwXE&index=3&list= PLgCYzUzKIBE8KHMzpp6JITZ2JxTgWqDH2 运行教程中的这个例子(源代码)。我检查了我的 Android 清单和 MainActivity。我没有更多了。

我的安卓清单:

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


    <uses-feature android:name="android.hardware.bluetooth" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>

我在 MainActivity 中的源代码:

public class MainActivity extends AppCompatActivity {

BluetoothAdapter bluetoothAdapter;
private Button mFind;
    private Switch mBluetooth;

    public ArrayList<BluetoothDevice> mBTDevices = new ArrayList<>();
    public DeviceListAdapter mdeviceListAdapter;
    ListView lnewDevices;
    private  BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            Log.d("MainActivity", "RUNNING-5");

            if (action.equals(BluetoothDevice.ACTION_FOUND)){
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                mBTDevices.add(device);
                Log.d("MainActivity", "Device " + device.getName());
                Log.d("MainActivity", "RUNNING-6");

                mdeviceListAdapter  = new DeviceListAdapter(context,R.layout.device_list_adapter_view, mBTDevices);
                lnewDevices.setAdapter(mdeviceListAdapter);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
   mBluetooth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mBluetooth.isChecked())
                {
                RunBluetooth();}
                if (!mBluetooth.isChecked())
                {
                    Log.d("MainActivity","I run off");
                }
            }
        });
        mFind = (Button) findViewById(R.id.mFind);
        mFind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DiscoverDevice();
            }
        });
}


    public void RunBluetooth()
    {

        if(!bluetoothAdapter.isEnabled())
        {
                Log.d("MainActivity", "run-1");
                Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBTIntent,1);
                Log.d("MainActivity", "run-2");

        }
        if(bluetoothAdapter.isEnabled())
        {
            DiscoverDevice();
        }
    }
    public void DiscoverDevice(){
        Log.d("MainActivity", "RUNNING-1");

        if (bluetoothAdapter.isDiscovering()){
            Log.d("MainActivity", "RUNNING-2");

            bluetoothAdapter.cancelDiscovery();
            bluetoothAdapter.startDiscovery();

            IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            registerReceiver(mBroadcastReceiver, discoverDevicesIntent);
        }
        if (!bluetoothAdapter.isDiscovering())
        {
            Log.d("MainActivity", "RUNNING-3");

            bluetoothAdapter.startDiscovery();
            IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            registerReceiver(mBroadcastReceiver, discoverDevicesIntent);
            Log.d("MainActivity", "RUNNING-4");

        }
    }}

如您所见 - 我在应用程序中有原始调试器。当我运行应用程序并单击一个按钮时,在我的 Logcat 中是:

05-27 15:22:06.005 9021-9021/com.example.kacper.kaccar D/MainActivity: RUNNING-1
05-27 15:22:06.016 9021-9021/com.example.kacper.kaccar D/MainActivity: RUNNING-3
05-27 15:22:06.025 9021-9021/com.example.kacper.kaccar D/MainActivity: RUNNING-4

我尝试添加这个:

IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mBroadcastReceiver3, discoverDevicesIntent);

保护 void onCreate 但我只有

05-27 14:22:06.025 9021-9021/com.example.kacper.kaccar D/MainActivity: RUNNING-5

标签: javaandroidbluetooth

解决方案


在设置 onClickListener 之前,您需要在 onCreate 中初始化 mBluetooth 开关。您应该在 onCreate() 中的 setContentView() 之后注册 Receiver,并在 onDestroy() 中取消注册 Receiver。


推荐阅读