public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG,"啟動service");
initReceiver();
bTAdatper = BluetoothAdapter.getDefaultAdapter();
if (!bTAdatper.isDiscovering()){ bTAdatper.startDiscovery();}
if (list_main!=null&&list_main.size()!=0){
Intent intent1=new Intent();
Bundle bundle=new Bundle();
System.out.println("service"+list_main.size());
bundle.putParcelableArrayList("123", (ArrayList<? extends Parcelable>) list_main);
intent1.putExtras(bundle);
intent1.setAction("com.ljq.activity.CountService");
sendBroadcast(intent1);
}
mCalendar = Calendar.getInstance();
mCalendar.setTimeInMillis(System.currentTimeMillis());
mCalendar.add(Calendar.SECOND, INTERVAL_TIME);//10秒后執行
//通過AlarmManager定時啟動廣播
alarmManager= (AlarmManager) getSystemService(ALARM_SERVICE);
Intent timeTaskIntent=new Intent(this, AlarmReceiver.class);
pIntent=PendingIntent.getBroadcast(this,0,timeTaskIntent ,PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), INTERVAL_TIME, pIntent);
return super.onStartCommand(intent, flags, startId);
}
private void initReceiver() {
//注冊廣播
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
}
//廣播接收器
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//避免重復添加已經系結過的設備
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
//adapter.add(device);
list_Device.add(device);
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
Log.e(TAG,"開始搜索");
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Log.e(TAG,"搜索完畢");
list_main=list_Device;
}
}
};
uj5u.com熱心網友回復:
就不知道斷點輸出log除錯么private void getBlueList() {
new Thread(new Runnable() {
@Override
public void run() {
bluetoothAdapter.startDiscovery();
}
}).start();
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null)
return;
LogUtils.e(intent.getAction());
switch (intent.getAction()) {
case BluetoothDevice.ACTION_FOUND:
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
LogUtils.e(" getAddress: " + bluetoothDevice.getAddress() + " name: " + bluetoothDevice.getName()
+ bluetoothDevice.getBondState() + " " + bluetoothDevice.getType() + " " + new Gson().toJson(bluetoothDevice.getUuids()));
if (bluetoothDevice != null) {
names.add(bluetoothDevice.getName() + "-" + bluetoothDevice.getAddress());
}
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
LemonBubble.forceHide();
if (names.size() == 0) {
Toast.makeText(BlueListActivity.this, "未發現可連接的藍牙設備", Toast.LENGTH_SHORT).show();
} else
arrayAdapter.notifyDataSetChanged();
break;
}
}
};
uj5u.com熱心網友回復:
我在fragment中沒有問題的,放在service中找不到這個android.bluetooth.device.action.FOUNDuj5u.com熱心網友回復:
我的設計終于搞完了,謝謝大佬

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/92784.html
標籤:Android
