设备列表未显示在通过蓝牙连接的一加6中

时间:2019-04-05 09:46:37

标签: java android bluetooth

您好,以下代码通过蓝牙显示附近的设备。 以下代码对于除一部手机和一部6部手机以外的所有设备均正常运行。

我在1加6手机中发现了一个错误。如果我们打开蓝牙并打开位置,则会列出附近设备的列表。

有谁能帮助我,特别是在一部手机上重新调试该错误。

public class DeviceScanActivity extends AppCompatActivity {
    private static final String TAG = DeviceScanActivity.class.getSimpleName();
    private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;
    @Bind(R.id.back)
    TextView mBack;
    @Bind(R.id.refresh)
    TextView mRefresh;
    @Bind(R.id.toolBar)
    Toolbar mToolBar;
    @Bind(R.id.scan_status)
    TextView mScanStatus;
    @Bind(R.id.deviceListView)
    RecyclerView mDeviceListView;
    @Bind(R.id.scanningProgress)
    ProgressBar mScanningProgress;
    private DeviceListAdapter mAdapter;
    private BleService mBleService;
    private List<BluetoothDevice> mBleDevices;
    private SharedPreferences mPref;
    private ProgressDialog mProgressDialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_device);
        ButterKnife.bind(this);
        mProgressDialog = new ProgressDialog(this);
        mPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        initToolBar();
        mBleDevices = new ArrayList<>();
        mAdapter = new DeviceListAdapter(this);
        mDeviceListView.setAdapter(mAdapter);
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mDeviceListView.setLayoutManager(manager);
        mDeviceListView.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                showProgressDialog();
                List<BluetoothDevice> bluetoothDevices = mAdapter.getBluetoothDevices();
                if (bluetoothDevices != null) {
                    if (bluetoothDevices.size() > position) {
                        BluetoothDevice bluetoothDevice = bluetoothDevices.get(position);
                        if (bluetoothDevice != null) {
                            String address = bluetoothDevice.getAddress();
                            mBleService.connect(address);
                        }
                    }
                }
            }
        }));
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BleService.ACTION_DEVICE_FOUND);
        intentFilter.addAction(BleService.ACTION_GATT_CONNECTED);
        intentFilter.addAction(BleService.ACTION_GAT_CONNECTING);
        intentFilter.addAction(BleService.ACTION_GATT_DISCONNECTED);
        intentFilter.addAction(BleService.ACTION_GAT_SERVICE_DISCOVERED);
        registerReceiver(mGattUpdateReceiver, intentFilter);

        if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.BLUETOOTH,Manifest.permission.ACCESS_FINE_LOCATION
                        , Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
            }
        } else {
            if (mServiceConnection != null) {
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, 101);
            }
            Intent intent = new Intent(this, BleService.class);
            bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
        }
    }
    private void showProgressDialog() {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
        }
        mProgressDialog.setMessage(getString(R.string.loading));
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.show();
    }
    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            mBleService = ((BleService.LocalBinder) iBinder).getLeService();
            if (mBleService != null) {
                mBleService.scanLeDevice(true);
                mScanStatus.setVisibility(View.VISIBLE);
                mScanStatus.setText(R.string.scanning_device);
            }
        }
        @Override
        public void onServiceDisconnected(ComponentName componentName) {
        }
    };
    @Override
    protected void onResume() {
        super.onResume();
    }
    @Override
    protected void onStart() {
        super.onStart();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 101) {
            if (mBleService != null) {
                mBleService.scanLeDevice(true);
            }
        }
    }
    private BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case BleService.ACTION_DEVICE_FOUND:
                    BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BleService.EXTRA_DEVICE);
                    if (mBleDevices != null) {
                        if (!mBleDevices.contains(device)) {
                            mBleDevices.add(device);
                            mAdapter.udpateBluetoothDevices(mBleDevices);
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                    break;
                case BleService.ACTION_GATT_CONNECTED:
                    Log.d(TAG, "!Action gat connected...");
                    mBleService.scanLeDevice(false);
                    mScanStatus.setVisibility(View.VISIBLE);
                    mScanStatus.setText(getString(R.string.connected));
                    break;
                case BleService.ACTION_GAT_CONNECTING:
                    mScanStatus.setVisibility(View.VISIBLE);
                    mScanStatus.setText(getString(R.string.connecting));
                    Log.d(TAG, "!Action Gat Connecting..");
                    break;
                case BleService.ACTION_GATT_DISCONNECTED:
                    mScanStatus.setVisibility(View.VISIBLE);
                    mScanStatus.setText(getString(R.string.disconnected));
                    mScanningProgress.setVisibility(View.GONE);
                    mRefresh.setVisibility(View.VISIBLE);
                    if (mProgressDialog != null) {
                        mProgressDialog.dismiss();
                    }
                    Log.d(TAG, "!Action Gat Disconnected..");
                    break;
                case BleService.ACTION_GAT_SERVICE_DISCOVERED:
                    boolean isOperator = mPref.getBoolean(Constants.IS_OPERATOR, false);
                    if (mProgressDialog != null) {
                        mProgressDialog.dismiss();
                    }
                    if (isOperator) {
                        Intent homeIntent = new Intent(DeviceScanActivity.this, HomeScreenActivity.class);
                        startActivity(homeIntent);
                        finish();
                    } else {
                        Intent lightControllIntent = new Intent(DeviceScanActivity.this, LightConfigurationActivity.class);
                        startActivity(lightControllIntent);
                        finish();
                    }
                    mScanStatus.setText(getString(R.string.discoveringService));
                    Log.d(TAG, "!Action Gat Discovering..");
                    break;
            }
        }
    };
    private void initToolBar() {
        mRefresh.setVisibility(View.VISIBLE);
    }
    @OnClick(R.id.refresh)
    public void onClick() {
        mScanStatus.setVisibility(View.VISIBLE);
        mRefresh.setVisibility(View.GONE);
        mScanningProgress.setVisibility(View.VISIBLE);
        mBleDevices.clear();
        mAdapter.udpateBluetoothDevices(new ArrayList<BluetoothDevice>());
        mAdapter.notifyDataSetChanged();
    }
    @Override
    protected void onDestroy() {
        if (mServiceConnection != null) {
            unbindService(mServiceConnection);
        }
        if (mGattUpdateReceiver != null) {
            unregisterReceiver(mGattUpdateReceiver);
        }
        super.onDestroy();
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == PERMISSION_REQUEST_COARSE_LOCATION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (mServiceConnection != null) {
                    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableBtIntent, 101);
                }
                Intent intent = new Intent(this, BleService.class);
                bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
            } else {
                finish();
            }
        }
    }
}

0 个答案:

没有答案