【发布时间】:2020-07-21 14:36:40
【问题描述】:
所以我正在构建一个具有内置蓝牙发现功能的应用程序。现在我正在两部不同的手机上试用:
- 手机 1:小米 9T Pro / Android 版本:9 = 运行良好
- Phone2:POCO F2 PRO / Android 版本:10 = 根本不工作
问题是,当我在电话 1 上试用时,控制台显示发现过程已开始,并向我显示附近有哪些设备,如下所示:
I/Timeline: Timeline: Activity_launch_request time:6873263 intent:Intent { act=android.content.pm.action.REQUEST_PERMISSIONS pkg=com.google.android.packageinstaller (has extras) }
I/System.out: Discoverybool was set to: true
D/MainActivity: onReceive: Action Found
D/MainActivity: onReceive: null: 9C:20:7B:CD:EF:75
D/MainActivity: onReceive: Action Found
D/MainActivity: onReceive: POCO: 98:F6:21:C0:2E:FF
D/MainActivity: onReceive: Action Found
onReceive: lieberto: 04:ED:33:63:9E:39
D/MainActivity: onReceive: Action Found
onReceive: MEDION TV: 70:54:B4:19:31:65
D/MainActivity: onReceive: Action Found
但是当我在电话 2 上试用它时:它就停在这里并且没有显示任何设备:
D/MainActivity: btnDiscover: Looking for unpaired Devices
I/System.out: Discoverybool was set to: true
有人知道问题出在哪里吗?提前感谢您的每一个回答!
这是我的代码:
Android.Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.fernsteuerung">
<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"
tools:ignore="ProtectedPermissions" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="NameOfApp"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
>
<activity android:name=".HistoryActivity" android:screenOrientation="portrait" />
<activity android:name=".TerminalActivity" android:screenOrientation="portrait" />
<activity android:name=".RemoteControl" android:screenOrientation="portrait"/>
<activity android:name=".ChooseActivity" android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
发现功能和相关的广播功能:
public void btnDiscover(View view)
{
//Checken ob Bluetooth an ist
if (mBluetoothAdapter == null) // Wenn das Gerät keinen Bluetooth Adapter hat
{
Log.d(TAG,"enableDisableBT: Does not have Bluetooth capabilities");
}
if(!(mBluetoothAdapter.isEnabled())) // Wenn Bluetooth ausgeschaltet ist
{
Log.d(TAG,"enableDisableBT: enabling BT.");
Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableBTIntent);
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBroadcastReceiver1, BTIntent);
}
Log.d(TAG,"btnDiscover: Looking for unpaired Devices");
if(mBluetoothAdapter.isDiscovering())
{
mBluetoothAdapter.cancelDiscovery();
Log.d(TAG,"btnDiscover:Cancelling discovery.");
//Checkt ob die Berechtigungen im Manifest für BT vorliegen
checkBTPermissions();
//start discovery again
mBluetoothAdapter.startDiscovery();
//scann(mBluetoothAdapter,30);
//System.out.println("scann1 was aufgerufen");
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver3,discoverDevicesIntent);
}
if(!(mBluetoothAdapter.isDiscovering()))
{
//another check
checkBTPermissions();
mBluetoothAdapter.startDiscovery();
//scann(mBluetoothAdapter,30);
//System.out.println("scann2 wurde aufgerufen");
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver3,discoverDevicesIntent);
}
discoverybool = true;
System.out.println("Discoverybool was set to: " + discoverybool);
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void checkBTPermissions() {
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION");
if (permissionCheck != 0) {
this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number
}
}else{
Log.d(TAG, "checkBTPermissions: No need to check permissions. SDK version < LOLLIPOP.");
}
}
相关广播:
// Create a BroadcastReceiver for ACTION_FOUND
private final BroadcastReceiver mBroadcastReceiver1 = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, mBluetoothAdapter.ERROR);
switch(state){
case BluetoothAdapter.STATE_OFF:
Log.d(TAG, "onReceive: STATE OFF");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log.d(TAG, "mBroadcastReceiver1: STATE TURNING OFF");
break;
case BluetoothAdapter.STATE_ON:
Log.d(TAG, "mBroadcastReceiver1: STATE ON");
break;
case BluetoothAdapter.STATE_TURNING_ON:
Log.d(TAG, "mBroadcastReceiver1: STATE TURNING ON");
break;
}
}
}
};
//Bluetooth Broadcoast receiver für den Button
private final BroadcastReceiver mBroadcastReceiver2 = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (action.equals(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED)) {
int mode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE,BluetoothAdapter.ERROR);
switch(mode)
{
//Wenn das Device in Discoverable Mode ist
case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE:
Log.d(TAG,"mBroadcastReceiver2: Discoverability Enabled.");
break;
//Wenn das Device nicht im discoverable mode ist:
case BluetoothAdapter.SCAN_MODE_CONNECTABLE:
Log.d(TAG,"mBroadcastReceiver2: Discoverability Disabled. Able to receive connections.");
break;
case BluetoothAdapter.SCAN_MODE_NONE:
Log.d(TAG,"mBroadcastReceiver2: Discoverability Disabled. Not able to receive connections.");
break;
case BluetoothAdapter.STATE_CONNECTING:
Log.d(TAG,"mBroadcastReceiver2: Connecting...");
break;
case BluetoothAdapter.STATE_CONNECTED:
Log.d(TAG,"mBroadcastReceiver2:Connected.");
break;
}
}
}
};
//Broadcast Receiver 3 für discover devices list
private BroadcastReceiver mBroadcastReceiver3 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.d(TAG,"onReceive: Action Found");
if(action.equals(BluetoothDevice.ACTION_FOUND))
{
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(TAG, "onReceive: " + device.getName() + ": " + device.getAddress());
if(device.getName() != null)
{
if(device.getName().contains("HC")) {
if(mBTDevices.size() < 1 ) {
mBTDevices.add(device);
mDeviceListAdapter = new DeviceListAdapter(context, R.layout.device_adapter_view, mBTDevices);
lvNewDevices.setAdapter((mDeviceListAdapter));
}
}
}
}
/*else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Log.v(TAG, "Entered the Finished ");
System.out.println("Entered the Finished");
}
*/
}
};
类 DeviceListAdapter.java
package com.example.fernsteuerung;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class DeviceListAdapter extends ArrayAdapter<BluetoothDevice> {
private LayoutInflater mLayoutInflater;
private ArrayList<BluetoothDevice> mDevices;
private int mViewResourceId;
public DeviceListAdapter(Context context, int tvResourceId, ArrayList<BluetoothDevice> devices){
super(context, tvResourceId,devices);
this.mDevices = devices;
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mViewResourceId = tvResourceId;
}
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mLayoutInflater.inflate(mViewResourceId, null);
BluetoothDevice device = mDevices.get(position);
if (device != null) {
TextView deviceName = (TextView) convertView.findViewById(R.id.tvDeviceName);
TextView deviceAdress = (TextView) convertView.findViewById(R.id.tvDeviceAddress);
if (deviceName != null) {
deviceName.setText(device.getName());
}
if (deviceAdress != null) {
deviceAdress.setText(device.getAddress());
}
}
return convertView;
}
}
【问题讨论】:
标签: java android android-studio bluetooth android-version