蓝牙 issue--定制app无需 location也可以使用

当不开启GPS时,系统仍能正常使用定制的APP进行BLE蓝牙扫描。问题源于Android Bluetooth GattService中的onScanResultInternal方法内对权限的检查。为解决此issue,可以通过添加特定设备白名单,绕过权限判断,使扫描功能正常运行。实现这一方案需修改GattService.java的相关代码逻辑。
摘要由CSDN通过智能技术生成

问题原因:

不打开GPS可正常调用上次系统定制的APP通过BLE也可以扫描

思路:

添加白名单的方式

参考的文件:

packages/apps/Bluetooth/src/com/android/bluetooth/gatt/GattService.java

在void onScanResultInternal方法中,有针对权限的判断

​1144      void onScanResultInternal(int eventType, int addressType, String address, int primaryPhy,
1145              int secondaryPhy, int advertisingSid, int txPower, int rssi, int periodicAdvInt,
1146              byte[] advData) {
...
boolean hasPermission = hasScanResultPermission(client);//判断是否打开了location权限
...
}
1298      private boolean hasScanResultPermission(final ScanClient client) {
1299          if (client.hasNetworkSettingsPermission
1300                  || client.hasNetworkSetupWizardPermission
1301  
安卓蓝牙APP通讯。package com.mt.truthblue2_1; import java.util.ArrayList; import java.util.List; import com.mt.mtblesdk.MTBeacon; import com.mt.tools.Tools; import com.mt.truthblue2_1.BLEService.LocalBinder; import android.annotation.SuppressLint; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { private final static int REQUEST_ENABLE_BT = 2001; private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { } @Override public void onServiceConnected(ComponentName name, IBinder service) { LocalBinder binder = (LocalBinder) service; Tools.mBLEService = binder.getService(); if (Tools.mBLEService.initBle()) { // scanBle(); // 开始扫描设备 if (!Tools.mBLEService.mBluetoothAdapter.isEnabled()) { final Intent enableBtIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } else { scanBle(); // 开始扫描设备 } } } }; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_ENABLE_BT) { if (resultCode == RESULT_OK) { scanBle(); // 开始扫描设备 } else { // finish(); } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); bindService(new Intent(this, BLEService.class), connection, Context.BIND_AUTO_CREATE); } // 初始化控件 private LayoutInflater mInflater; private ListView ble_listview; private List<MTBeacon> scan_devices = new ArrayList<MTBeacon>(); private List<MTBeacon> scan_devices_dis = new ArrayList<MTBeacon>(); private BaseAdapter list_adapter = new BaseAdapter() { @SuppressLint("InflateParams") @Override public View getView(int position, View convertView, ViewGroup parent) { if (null == convertView) { convertView = mInflater.inflate(R.layout.devicefmt, null); } TextView device_name_txt = (TextView) convertView .findViewById(R.id.device_name_txt); TextView device_rssi_txt = (TextView) convertView .findViewById(R.id.device_rssi_txt); TextView device_mac_txt = (TextView) convertView .findViewById(R.id.device_mac_txt); device_name_txt.setText(getItem(position).GetDevice().getName()); device_mac_txt.setText("Mac: " + getItem(position).GetDevice().getAddress()); device_rssi_txt.setText("Rssi: " + getItem(position).GetAveragerssi()); return convertView; } @Override public long getItemId(int position) { return position; } @Override public MTBeacon getItem(int position) { return scan_devices_dis.get(position); } @Override public int getCount() { return scan_devices_dis.size(); } }; private void initView() { mInflater = LayoutInflater.from(this); ble_listview = (ListView) findViewById(R.id.ble_listview); ble_listview.setAdapter(list_adapter); ble_listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // scan_flag = false; // Tools.mBLEService.stopscanBle(mLeScanCallback); Intent intent = new Intent(getApplicationContext(), ServiceActivity.class); intent.putExtra("device", scan_devices_dis.get(position) .GetDevice()); startActivity(intent); } }); } // 开始扫描 private int scan_timer_select = 0; private boolean scan_flag = true; private Handler search_timer = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); search_timer.sendEmptyMessageDelayed(0, 500); if (!scan_flag) { return; } if (!Tools.mBLEService.mBluetoothAdapter.isEnabled()) { return; } // 扫描时间调度 switch (scan_timer_select) { case 1: // 开始扫描 Tools.mBLEService.scanBle(mLeScanCallback); break; case 3: // 停止扫描(结算) Tools.mBLEService.stopscanBle(mLeScanCallback); // 停止扫描 for (int i = 0; i < scan_devices.size();) { // 防抖 if (scan_devices.get(i).CheckSearchcount() > 2) { scan_devices.remove(i); } else { i++; } } scan_devices_dis.clear(); // 显示出来 for (MTBeacon device : scan_devices) { scan_devices_dis.add(device); } list_adapter.notifyDataSetChanged(); break; default: break; } scan_timer_select = (scan_timer_select + 1) % 4; } }; private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { int i = 0; // 检查是否是搜索过的设备,并且更新 for (i = 0; i < scan_devices.size(); i++) { if (0 == device.getAddress().compareTo( scan_devices.get(i).GetDevice().getAddress())) { scan_devices.get(i).ReflashInf(device, rssi, scanRecord); // 更新信息 return; } } // 增加新设备 scan_devices.add(new MTBeacon(device, rssi, scanRecord)); } }; private void scanBle() { search_timer.sendEmptyMessageDelayed(0, 500); } @Override protected void onPause() { super.onPause(); scan_flag = false; Tools.mBLEService.stopscanBle(mLeScanCallback); } @Override protected void onResume() { super.onResume(); scan_flag = true; } @Override protected void onDestroy() { super.onDestroy(); unbindService(connection); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

专业开发者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值