接收广播Intent时出错{act = android.bluetooth.device.action.FOUND flg = 0x10} [英] Error receiving broadcast Intent { act=android.bluetooth.device.action.FOUND flg=0x10}
问题描述
我搜索了,但没有发现任何类似的东西.我正在使用Android中的蓝牙开发两个设备之间的连接.在ACTION_FOUND方法上,当我尝试使设备信息出现在listView上时,我在logcat上收到了该错误.
I searched and i didn't find anything symilar. I'm developing a connection between two defices using Bluetooth in Android. And on ACTION_FOUND method, when i tried to make the device information appear on listView, i got that error on logcat.
DesafioActivity: 公共类DesafioActivity扩展了活动{
DesafioActivity: public class DesafioActivity extends Activity {
private TextView nomeDispositivo;
private TextView MAC_Adress;
private BluetoothAdapter bthAdapter = BluetoothAdapter.getDefaultAdapter();
private ArrayAdapter<String> bthDispositivosArea;
private ListView lv_devicesArea;
private IntentFilter filter;
private Button btn_servidor, btn_cliente;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_desafio);
nomeDispositivo = (TextView)findViewById(id.tv_apresentaNomeDispositivo);
MAC_Adress = (TextView)findViewById(id.tv_apresentaMacAdress);
lv_devicesArea = (ListView)findViewById(id.lv_apresentaDispositivosDescobertos);
VerificarEstadoBth();
nomeDispositivo.setText(bthAdapter.getName());
MAC_Adress.setText(bthAdapter.getAddress());
//Regista o BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_UUID);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(HandleDiscovery, filter); // Don't forget to unregister during onDestroy
bthAdapter.startDiscovery();
}
public void VerificarEstadoBth(){
//Bluetooth não suportado
if (bthAdapter == null) {
Toast.makeText(this, "DEVIDE DOESN'T SUPPORT", Toast.LENGTH_LONG).show();
}
// Ativa e torna o dispositivo visível para conexão
if (!bthAdapter.isDiscovering()) {
Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
Toast.makeText(getApplicationContext(),"AVAIABLE FOR CONNECTION" ,Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(),"AVAIABLE FOR CONNECTION",Toast.LENGTH_SHORT).show();
}
}
private final BroadcastReceiver HandleDiscovery = 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);
Toast.makeText(getApplicationContext(),"DEVICE FOUND! " ,Toast.LENGTH_LONG).show();
bthDispositivosArea.add("\n Device: " + device.getName() + ", " + device);
lv_devicesArea.setAdapter(bthDispositivosArea);
} else {
if(BluetoothDevice.ACTION_UUID.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
for (int i=0; i<uuidExtra.length; i++) {
//out.append("\n Device: " + device.getName() + ", " + device + ", Service: " + uuidExtra[i].toString());
}
} else {
if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
Toast.makeText(getApplicationContext(),"START DISCOVERY!",Toast.LENGTH_SHORT).show();
}
}
}
}
};
@Override
public void onDestroy() {
unregisterReceiver(HandleDiscovery);
super.onDestroy();
}
}
LogCat:
12-30 16:33:24.657: E/AndroidRuntime(12794): FATAL EXCEPTION: main
12-30 16:33:24.657: E/AndroidRuntime(12794): Process: com.AMOV.mr.fit, PID: 12794
12-30 16:33:24.657: E/AndroidRuntime(12794): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.device.action.FOUND flg=0x10 (has extras) } in com.AMOV.mr.fit.DesafioActivity$1@41a98b68
12-30 16:33:24.657: E/AndroidRuntime(12794): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:776)
12-30 16:33:24.657: E/AndroidRuntime(12794): at android.os.Handler.handleCallback(Handler.java:733)
12-30 16:33:24.657: E/AndroidRuntime(12794): at android.os.Handler.dispatchMessage(Handler.java:95)
12-30 16:33:24.657: E/AndroidRuntime(12794): at android.os.Looper.loop(Looper.java:136)
12-30 16:33:24.657: E/AndroidRuntime(12794): at android.app.ActivityThread.main(ActivityThread.java:5146)
12-30 16:33:24.657: E/AndroidRuntime(12794): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 16:33:24.657: E/AndroidRuntime(12794): at java.lang.reflect.Method.invoke(Method.java:515)
12-30 16:33:24.657: E/AndroidRuntime(12794): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
12-30 16:33:24.657: E/AndroidRuntime(12794): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
12-30 16:33:24.657: E/AndroidRuntime(12794): at dalvik.system.NativeStart.main(Native Method)
12-30 16:33:24.657: E/AndroidRuntime(12794): Caused by: java.lang.NullPointerException
12-30 16:33:24.657: E/AndroidRuntime(12794): at com.AMOV.mr.fit.DesafioActivity$1.onReceive(DesafioActivity.java:112)
12-30 16:33:24.657: E/AndroidRuntime(12794): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:766)
12-30 16:33:24.657: E/AndroidRuntime(12794): ... 9 more
推荐答案
bthDispositivosArea,您的ArrayAdapter为null,因为您还没有在任何地方对其进行初始化.并且您正在尝试向空对象添加一些东西.
bthDispositivosArea, your ArrayAdapter is null because you haven't initialized it anywhere. And you're trying to add something to a null object.
这篇关于接收广播Intent时出错{act = android.bluetooth.device.action.FOUND flg = 0x10}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!