接收广播错误蓝牙发现 - 错误onRecieve()方法,通过startDiscovery接收意图()方法 [英] Receiving broadcast error for Bluetooth discovery - Error in onRecieve() method that receives intents by startDiscovery() Method
问题描述
您好我是新来的Android编程,我面临着设备扫描活动的一个问题。我想扫描
所有蓝牙设备
在附近,并且打印出来的UI。但我得到相同的运行时错误在<一个href=\"http://stackoverflow.com/questions/10376388/receiving-broadcast-error-for-bluetooth-discovery\">Receiving广播错误的蓝牙发现。请请请帮我调试我的code。
公共类MainActivity扩展ActionBarActivity { BluetoothAdapter mBluetoothAdapter;
私人INT REQUEST_ENABLE_BT = 1000;
公众的ArrayList&LT;串GT; deviceArrayList =新的ArrayList&LT;&GT;();
公众的ArrayList&LT;&BluetoothDevice类GT; DEVICELIST =新的ArrayList&LT;&GT;(); @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
} @覆盖
保护无效onResume(){
// TODO自动生成方法存根
super.onResume();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
如果(mBluetoothAdapter == NULL){
//设备不支持蓝牙
Toast.makeText(这一点,设备不支持蓝牙!!!,Toast.LENGTH_LONG).show();
返回;
}
如果(!mBluetoothAdapter.isEnabled()){
意图enableBtIntent =新意图(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);
}
scanStart();
} 公共无效scanStart(){
// TODO自动生成方法存根 //添加配对设备到ArrayList中
addPairedDevices(); 如果(mBluetoothAdapter.isDiscovering()){
mBluetoothAdapter.cancelDiscovery();
}
如果(mBluetoothAdapter.startDiscovery()==真){
Toast.makeText(这一点,搜索...,Toast.LENGTH_LONG).show();
}
其他{
Toast.makeText(这一点,蓝牙适配器了NULL值,Toast.LENGTH_SHORT).show();
} //注册的BroadcastReceiver
IntentFilter的过滤器=新的IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(requestReciever,过滤器); //不要忘记的onDestroy期间注销 尝试{
视频下载(12000);
}赶上(InterruptedException的E){
// TODO自动生成catch块
e.printStackTrace();
} 在UI //打印设备列表
ListView控件LV =新的ListView(这一点);
ArrayAdapter&LT;串GT; listAdapter =新ArrayAdapter&LT;&GT;(这一点,R.layout.print_device_list,deviceArrayList);
lv.setAdapter(listAdapter);
的setContentView(LV);
如果(deviceArrayList.size()&下; = 0){
Toast.makeText(这一点,未找到设备...,Toast.LENGTH_LONG).show();
}
}
公共无效addPairedDevices(){
// TODO自动生成方法存根
//获取配对设备
SET&LT;&BluetoothDevice类GT; pairedDevices = mBluetoothAdapter.getBondedDevices();
//如果有配对设备
如果(pairedDevices.size()大于0){
//循环配对设备
对于(BluetoothDevice类设备:pairedDevices){
//添加名称和地址到一个数组适配器在ListView显示
deviceArrayList.add(device.getName()+\\ n+ device.getAddress()+\\ n+ device.getUuids()[0] .getUuid());
deviceList.add(设备);
}
}
}
私人广播接收器requestReciever =新的广播接收器(){ 公共无效的onReceive(上下文的背景下,意图意图){
如果(意向== NULL){
返回;
}
字符串行动= intent.getAction(); 如果(BluetoothDevice.ACTION_FOUND.equals(动作)){
Log.d(BT,设备中找到);
BluetoothDevice类设备= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
如果(设备!= NULL){
如果(device.getName()=空&放大器;!&放大器; device.getName()长度()大于0){
deviceArrayList.add(device.getName()+\\ n+ device.getAddress()+\\ n+ device.getUuids()[0] .getUuid());
deviceList.add(设备);
}
}
}
};
};
@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
// TODO自动生成方法存根
super.onActivityResult(要求code,结果code,数据); 如果(要求code == REQUEST_ENABLE_BT){
如果(结果code == Activity.RESULT_CANCELED){
Toast.makeText(这一点,UESR想不打开蓝牙!!!,Toast.LENGTH_LONG).show();
返回;
}
}
} @覆盖
保护无效的onDestroy(){
// TODO自动生成方法存根
super.onDestroy();
如果(mBluetoothAdapter!= NULL){
mBluetoothAdapter.cancelDiscovery();
}
如果(requestReciever!= NULL)
unregisterReceiver(requestReciever);
Toast.makeText(这一点,蓝牙应用程序已停止,Toast.LENGTH_LONG).show();
}
}
device.getUuids()
- 在这里你有一个空,这就是为什么你在这里有一个错误。
检查这个变量beforе使用它。
Hi I am new to android programming and i am facing a problem in device scan activity. I want to scan
all bluetooth devices
in vicinity and print them on UI. But i am getting the same run time error as in Receiving broadcast error for Bluetooth discovery. Please, please please help me to debug my code.
public class MainActivity extends ActionBarActivity {
BluetoothAdapter mBluetoothAdapter;
private int REQUEST_ENABLE_BT = 1000;
public ArrayList<String> deviceArrayList = new ArrayList<>();
public ArrayList<BluetoothDevice> deviceList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
Toast.makeText(this, "Device Doesn't Support Bluetooth!!!", Toast.LENGTH_LONG).show();
return;
}
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT );
}
scanStart();
}
public void scanStart() {
// TODO Auto-generated method stub
//Adding Paired Devices into ArrayList
addPairedDevices();
if(mBluetoothAdapter.isDiscovering()){
mBluetoothAdapter.cancelDiscovery();
}
if(mBluetoothAdapter.startDiscovery() == true){
Toast.makeText(this, "Searching...", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this, "Bluetooth Adapter got Null Value", Toast.LENGTH_SHORT).show();
}
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(requestReciever, filter); // Don't forget to unregister during onDestroy
try {
Thread.sleep(12000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Printing Device List on UI
ListView lv = new ListView(this);
ArrayAdapter<String> listAdapter = new ArrayAdapter<>(this, R.layout.print_device_list, deviceArrayList);
lv.setAdapter(listAdapter);
setContentView(lv);
if(deviceArrayList.size()<=0){
Toast.makeText(this, "No Devices Found...", Toast.LENGTH_LONG).show();
}
}
public void addPairedDevices() {
// TODO Auto-generated method stub
// get paired devices
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
deviceArrayList.add(device.getName() + "\n" + device.getAddress() + "\n" + device.getUuids()[0].getUuid());
deviceList.add(device);
}
}
}
private BroadcastReceiver requestReciever = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent) {
if (intent == null) {
return;
}
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
Log.d("BT", "Device Found");
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(device!=null){
if(device.getName() != null && device.getName().length() > 0){
deviceArrayList.add(device.getName()+"\n"+device.getAddress()+"\n"+device.getUuids()[0].getUuid());
deviceList.add(device);
}
}
}
};
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_ENABLE_BT){
if(resultCode == Activity.RESULT_CANCELED){
Toast.makeText(this, "Uesr Doesn't want to switch on Bluetooth!!!", Toast.LENGTH_LONG).show();
return;
}
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(mBluetoothAdapter != null){
mBluetoothAdapter.cancelDiscovery();
}
if(requestReciever!=null)
unregisterReceiver(requestReciever);
Toast.makeText(this, "Bluetooth App Stopped", Toast.LENGTH_LONG).show();
}
}
device.getUuids()
- here you have a Null, that is why you have an error here.
Check this variable beforе using it.
这篇关于接收广播错误蓝牙发现 - 错误onRecieve()方法,通过startDiscovery接收意图()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!