android 蓝牙重复扫描

ti10na28 2016-09-23 06:22:01
我想在service内让蓝牙可以一直执行扫描,之前在activity内可以重复扫描,但移植到服务后就不能了请问要怎么解决?

public class search_beacon extends Service implements LocationListener {

HashMap<String, Integer> record = new HashMap<String, Integer>();
private Long startTime;
private Handler timer = new Handler();
LocationManager locationManager;
String best_loc;
double latitude;
double longitude;
String sql;

private BluetoothAdapter mBluetoothAdapter;
private int REQUEST_ENABLE_BT = 1234;
private Handler mHandler;
private static final long SCAN_PERIOD = 10000;
private BluetoothLeScanner mLEScanner;
private ScanSettings settings;
private List<ScanFilter> filters;


@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate(){
Log.i("search_beacon", "Enter onCreate()");
super.onCreate();
Log.i("search_beacon", "Leave onCreate()");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("search_beacon","Enter onStartCommand()");

//宣告倒數
mHandler = new Handler();
//ble
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, "BLE Not Supported",
Toast.LENGTH_SHORT).show();
}
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
//gps
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//set the location provider
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
best_loc = locationManager.getBestProvider(criteria, true);
if (best_loc != null) {
Location loc = locationManager.getLastKnownLocation(best_loc);
showlocation(loc);
}

//計時器設定
startTime = System.currentTimeMillis();
//設定定時要執行的方法
timer.removeCallbacks(updateTimer);
//設定Delay的時間
timer.postDelayed(updateTimer, 5000);


if (mBluetoothAdapter.isEnabled()) {
if (Build.VERSION.SDK_INT >= 21) {
mLEScanner = mBluetoothAdapter.getBluetoothLeScanner();

filters = new ArrayList<ScanFilter>();
settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.build();
}
scanLeDevice(true);
}

return Service.START_STICKY;
}


private void scanLeDevice(final boolean enable) {
if (enable) {

mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT < 21) {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
} else {
mLEScanner.stopScan(mScanCallback);
}
}
}, SCAN_PERIOD);

if (Build.VERSION.SDK_INT < 21) {
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mLEScanner.startScan(filters, settings, mScanCallback);
}
} else {
if (Build.VERSION.SDK_INT < 21) {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
} else {
mLEScanner.stopScan(mScanCallback);
}
}
}

String device_mac;
Integer device_rssi;
private ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.i("callbackType", String.valueOf(callbackType));
Log.i("result", result.toString());
BluetoothDevice btDevice = result.getDevice();
//connectToDevice(btDevice);
Log.i("jiejv",btDevice.getAddress());
device_mac = btDevice.getAddress();
device_rssi = result.getRssi();
String check_mac=device_mac.substring(0, 11); //第十一碼以前,不含十一
if(device_mac.equals(null) || device_rssi== null)
Log.i("jiejv",device_mac+"空空空"+device_rssi);
else if(check_mac.equals("01:17:C5:57"))
{
record.put(device_mac,device_rssi);
}

//if(update_record(device_mac,device_rssi))
Log.i("jiejv",device_mac+" "+device_rssi);
}

@Override
public void onBatchScanResults(List<ScanResult> results) {
for (ScanResult sr : results) {
Log.i("ScanResult - Results", sr.toString());
}
}

@Override
public void onScanFailed(int errorCode) {
Log.e("Scan Failed", "Error Code: " + errorCode);
}
};

private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {

@Override
public void onLeScan(final BluetoothDevice device, int rssi,
byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i("onLeScan", device.toString());
//connectToDevice(device);
}
});
}
};


//固定要執行的方法
private Runnable updateTimer = new Runnable() {
public void run() {

Iterator<?> it = record.entrySet().iterator();
while(it.hasNext())
{
Map.Entry me = (Map.Entry)it.next();
String mac = me.getKey().toString();
int rs = (int)me.getValue();
if(update_record(mac,rs))
{

Log.i("timer",mac+" "+rs);
// record.remove(me.getKey());
}

}
record.clear();
timer.postDelayed(this, 10000);
}
};

@Override
public void onDestroy() {
Log.i("search_beacon","Enter onDestroy()");
super.onDestroy();
handler1.removeCallbacks(showTime); //利用removeCallbacks()停止handler.post(showTime)
Log.i("search_beacon","Leave onDestroy()");
}
}
...全文
953 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
迈进一步 2017-04-10
  • 打赏
  • 举报
回复
你好,这个问题解决了吗?我发现是startLeScan() 这个方法是可以一直搜索蓝牙设备的,但是时间一长,就不会再扫描了。感觉是被手机杀死了这个东西,然后我自己做了一个重启功能,但是在service里面重启startLeScan(),不能重启。必须在打开这个APP才能重启。
ti10na28 2016-09-29
  • 打赏
  • 举报
回复
引用 3 楼 xiaoyaoyou1212 的回复:
[quote=引用 2 楼 ti10na28的回复:][quote=引用 1 楼 xiaoyaoyou1212 的回复:] 看你的代码应该是做BLE相关的,可以参考下http://blog.csdn.net/xiaoyaoyou1212/article/details/52346904,这个是使用介绍,在GIthub上有源码及demo演示,已通过实际项目验证。针对你这种情况,只需要注册一个服务,在服务中操作扫描等功能就行,希望对你有所帮助!
在service里扫描没有问题 但是想要让蓝牙可以在service里持续扫描这个动作我不会写 可以请大大帮我解答吗?[/quote] 我没太明白你的意思,蓝牙扫描的方法startLeScan是可以一直扫描的,除非你本身做了超时处理,进行了stopLeScan操作,其实你可以看看我那个蓝牙操作基础框架,关于扫描已经很清晰了,你按照提供的demo使用下应该很容易的,希望能解答你的疑惑![/quote]
引用 3 楼 xiaoyaoyou1212 的回复:
[quote=引用 2 楼 ti10na28的回复:][quote=引用 1 楼 xiaoyaoyou1212 的回复:] 看你的代码应该是做BLE相关的,可以参考下http://blog.csdn.net/xiaoyaoyou1212/article/details/52346904,这个是使用介绍,在GIthub上有源码及demo演示,已通过实际项目验证。针对你这种情况,只需要注册一个服务,在服务中操作扫描等功能就行,希望对你有所帮助!
在service里扫描没有问题 但是想要让蓝牙可以在service里持续扫描这个动作我不会写 可以请大大帮我解答吗?[/quote] 我没太明白你的意思,蓝牙扫描的方法startLeScan是可以一直扫描的,除非你本身做了超时处理,进行了stopLeScan操作,其实你可以看看我那个蓝牙操作基础框架,关于扫描已经很清晰了,你按照提供的demo使用下应该很容易的,希望能解答你的疑惑![/quote] 好的 我研究看看 谢谢你
幻影宇寰 2016-09-28
  • 打赏
  • 举报
回复
引用 2 楼 ti10na28的回复:
[quote=引用 1 楼 xiaoyaoyou1212 的回复:] 看你的代码应该是做BLE相关的,可以参考下http://blog.csdn.net/xiaoyaoyou1212/article/details/52346904,这个是使用介绍,在GIthub上有源码及demo演示,已通过实际项目验证。针对你这种情况,只需要注册一个服务,在服务中操作扫描等功能就行,希望对你有所帮助!
在service里扫描没有问题 但是想要让蓝牙可以在service里持续扫描这个动作我不会写 可以请大大帮我解答吗?[/quote] 我没太明白你的意思,蓝牙扫描的方法startLeScan是可以一直扫描的,除非你本身做了超时处理,进行了stopLeScan操作,其实你可以看看我那个蓝牙操作基础框架,关于扫描已经很清晰了,你按照提供的demo使用下应该很容易的,希望能解答你的疑惑!
ti10na28 2016-09-28
  • 打赏
  • 举报
回复
引用 1 楼 xiaoyaoyou1212 的回复:
看你的代码应该是做BLE相关的,可以参考下http://blog.csdn.net/xiaoyaoyou1212/article/details/52346904,这个是使用介绍,在GIthub上有源码及demo演示,已通过实际项目验证。针对你这种情况,只需要注册一个服务,在服务中操作扫描等功能就行,希望对你有所帮助!
在service里扫描没有问题 但是想要让蓝牙可以在service里持续扫描这个动作我不会写 可以请大大帮我解答吗?
幻影宇寰 2016-09-25
  • 打赏
  • 举报
回复
看你的代码应该是做BLE相关的,可以参考下http://blog.csdn.net/xiaoyaoyou1212/article/details/52346904,这个是使用介绍,在GIthub上有源码及demo演示,已通过实际项目验证。针对你这种情况,只需要注册一个服务,在服务中操作扫描等功能就行,希望对你有所帮助!

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧