public class Myservice extends Service {
private BluetoothAdapter bTAdatper;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
} @Override
public void onCreate() {
super.onCreate();
bTAdatper = BluetoothAdapter.getDefaultAdapter();
initReceiver();
new Thread(new Runnable() {
@Override
public void run() {
while (bTAdatper.isDiscovering()) {
System.out.println("11");
try {
Thread.sleep(1000);
bTAdatper.startDiscovery();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
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); }
} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
System.out.println("开始搜索");
//Toast.makeText(getActivity(), "开始搜索", Toast.LENGTH_SHORT).show();
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
System.out.println("搜索完毕");
}
}
};
}
private BluetoothAdapter bTAdatper;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
} @Override
public void onCreate() {
super.onCreate();
bTAdatper = BluetoothAdapter.getDefaultAdapter();
initReceiver();
new Thread(new Runnable() {
@Override
public void run() {
while (bTAdatper.isDiscovering()) {
System.out.println("11");
try {
Thread.sleep(1000);
bTAdatper.startDiscovery();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
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); }
} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
System.out.println("开始搜索");
//Toast.makeText(getActivity(), "开始搜索", Toast.LENGTH_SHORT).show();
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
System.out.println("搜索完毕");
}
}
};
}
解决方案 »
- Android 虚拟机的问题
- 太诡异了!点击SDK Manager居然说jdk找不到!
- 新手提问
- 愤怒小鸟中的广告是服务器端“推”的还是客户端“轮询”的?
- UriMatcher.addURI中的/#/*解释
- android计算器源代码
- 关于Android使用HttpPos方式带参数访问Asp.net网页问题
- 请教一下android的布局问题!!!
- 安卓编程的问题 为什么我在写布局文件的时候有些android: 的属性没有呢
- 创建新工程时没有创建MainActivity的窗口
- Android 相机预览用bitmap剪切身份证区域,如何适配所有手机?
- 第一次用androidstudio,请问这是什么错误,以前用eclipise没报过错
或者版本低的手机能运行么?
System.out.println("11");
try {
Thread.sleep(1000);
bTAdatper.startDiscovery();
} catch (InterruptedException e) {
e.printStackTrace();
}
不需要搞1秒延时, initReceiver();之后
if(!bTAdatper.isDiscovering()){ //判断没有在搜索就开始走索蓝牙
bTAdatper.startDiscovery();
}
另外,要会打日志断点调试找问题出现在哪里
registerReceiver(receiver, makeFilter()); @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
private void startSearch() { if (bluetoothAdapter == null) {
Logger.d("gxh", "device not support bluetooth");
swipeRefreshLayout.setRefreshing(false);
return;
} //判断蓝牙是否开启
if (bluetoothAdapter.isEnabled()) {
if (bluetoothAdapter.isDiscovering()) {
bluetoothAdapter.cancelDiscovery();
}
deviceMap.clear();
bluetoothAdapter.startDiscovery();
} else {
Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabler, 100);
} }
public class BlueToothReceiver extends 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);
deviceMap.put(device.getAddress(), device);
Logger.d("gxh", "found:" + device.getName());
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//搜索完毕
Logger.d("gxh", "ACTION_DISCOVERY_FINISHED");
swipeRefreshLayout.setRefreshing(false);
devices.clear();
for (Map.Entry<String, BluetoothDevice> entry : deviceMap.entrySet()) {
devices.add(entry.getValue());
}
blueConfigAdapter.setNewData(devices);
}
}
}
public IntentFilter makeFilter() {
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);//蓝牙状态改变的广播
filter.addAction(BluetoothDevice.ACTION_FOUND);//找到设备的广播
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//搜索完成的广播
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);//开始扫描的广播
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//状态改变
return filter;
}
这是我以前写的,可以用。