嗨朋友我正在做一个简单的蓝牙聊天应用程序,我无法通过意图将设备名称从一个BLE活动发送到另一个警报对话活动。在下一个活动中,我已取名并显示详细信息。 我已经在这里执行异步任务来显示渐进式对话框,因为我已经编写了配对设备代码。任何人都可以帮助解决这个问题。 你有很多需要。 Thnq: - )
我在异步任务中遇到此异常:
07-17 14:30:59.266: E/AndroidRuntime(3615): FATAL EXCEPTION: AsyncTask #1
07-17 14:30:59.266: E/AndroidRuntime(3615): Process: com.appzoy.bluetoothlowenergyapplication, PID: 3615
07-17 14:30:59.266: E/AndroidRuntime(3615): java.lang.RuntimeException: An error occured while executing doInBackground()
07-17 14:30:59.266: E/AndroidRuntime(3615): at android.os.AsyncTask$3.done(AsyncTask.java:300)
07-17 14:30:59.266: E/AndroidRuntime(3615): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-17 14:30:59.266: E/AndroidRuntime(3615): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-17 14:30:59.266: E/AndroidRuntime(3615): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-17 14:30:59.266: E/AndroidRuntime(3615): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-17 14:30:59.266: E/AndroidRuntime(3615): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-17 14:30:59.266: E/AndroidRuntime(3615): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-17 14:30:59.266: E/AndroidRuntime(3615): at java.lang.Thread.run(Thread.java:841)
07-17 14:30:59.266: E/AndroidRuntime(3615): Caused by: java.lang.NullPointerException
07-17 14:30:59.266: E/AndroidRuntime(3615): at com.appzoy.bluetoothlowenergyapplication.BLEChat$myAsyncTask.doInBackground(BLEChat.java:122)
07-17 14:30:59.266: E/AndroidRuntime(3615): at com.appzoy.bluetoothlowenergyapplication.BLEChat$myAsyncTask.doInBackground(BLEChat.java:1)
07-17 14:30:59.266: E/AndroidRuntime(3615): at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-17 14:30:59.266: E/AndroidRuntime(3615): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
我的名字是BLe,但它不仅仅是普通的蓝牙应用程序
package com.appzoy.bluetoothlowenergyapplication;
import java.util.ArrayList;
import java.util.Set;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class BLEChat extends Activity {
Button searching, send;
EditText devicelist,paircode;
private BluetoothAdapter mBluetoothAdapter;
private static final int REQUEST_ENABLE_BT=1;
ProgressDialog dialog;
private RadioButton changecolor;
TextView deviceadd;
Set<String> paired_devices;
ArrayList<String> Discovery_devices;
private boolean mScanning;
BluetoothManager bluetoothManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blechat);
searching = (Button) findViewById(R.id.finding);
send=(Button) findViewById(R.id.send);
devicelist=(EditText) findViewById(R.id.devicedetails);
paircode=(EditText) findViewById(R.id.paircode);
changecolor=(RadioButton) findViewById(R.id.display);
deviceadd=(TextView) findViewById(R.id.device_address);
Discovery_devices=new ArrayList<String>();
//searching of the bluetooth supportivity
bluetoothOperations();
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
this.registerReceiver(mReceiver, filter);
registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
//searching of the devices is happend through the background activity
searching.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new myAsyncTask().execute();
// if (mBluetoothAdapter.isEnabled()) {
//
//
//
//
// }else{
//
// Toast.makeText(BLEChat.this,"Tha application of bluetooth is stopped... ", 2).show();
// bluetoothOperations();
// }
}
});
paircode.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
private class Mytask extends AsyncTask<String,String, String> {
final String TAG = "BLEChat.Mytask";
protected void onPreExecute() {
Log.d(TAG,"executing inside of the preexecute method");
}
protected String doInBackground(String... params) {
return null;
}
protected void onPostExecute(String result) {
}
}
class myAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
Toast.makeText(BLEChat.this, "pre execute method", 1).show();
dialog = ProgressDialog.show(BLEChat.this, "",
getString(R.string.progress_dialog), true, true);
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
String str=device.getName();
Intent in = new Intent(BLEChat.this, Alert_Dialog.class);
in.putExtra("devicename",str);
startActivity(in);
}
mScanning=true;
}
return null;
}
@Override
protected void onPostExecute(Void result) {
Toast.makeText(BLEChat.this, "post execute method", 1).show();
dialog.dismiss();
if(mScanning=true){
Intent in = new Intent(BLEChat.this, Alert_Dialog.class);
startActivity(in);
}else{
Toast.makeText(BLEChat.this, "NO devices are found", 1).show();
}
super.onPostExecute(result);
}
}
public void bluetoothOperations(){
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "The bluetooth device not supporting", 1).show();
}
//checking wether it is enabled or not
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}else{
changecolor.setBackgroundColor(Color.YELLOW);
}
}
final BroadcastReceiver bReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// add the name and the MAC address of the object to the arrayAdapter
String str=device.getName()
Intent in = new Intent(BLEChat.this, Alert_Dialog.class);
in.putExtra("devicename",str);
startActivity(in);
}
}
};
@Override
protected void onPause() {
super.onPause();
if(dialog != null)
dialog.dismiss();
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_OFF:
changecolor.setBackgroundColor(Color.GRAY);
Log.v("log", "this is shown");
Toast.makeText(context, "bluetooth is off" , 2).show();
break;
case BluetoothAdapter.STATE_TURNING_OFF:
changecolor.setBackgroundColor(Color.GRAY);
Toast.makeText(context, "Turning Bluetooth off..." , 2).show();
break;
case BluetoothAdapter.STATE_ON:
changecolor.setBackgroundColor(Color.YELLOW);
Toast.makeText(context, "Bluetooth on..." , 2).show();
break;
case BluetoothAdapter.STATE_TURNING_ON:
changecolor.setBackgroundColor(Color.YELLOW);
Toast.makeText(context, "Turning Bluetooth on..." , 2).show();
break;
}
}
}
};
@Override
public void onDestroy() {
super.onDestroy();
/* ... */
// Unregister broadcast listeners
this.unregisterReceiver(mReceiver);
this.unregisterReceiver(bReceiver);
}
}
这是自定义警报对话框活动,我通过捆绑包从上面的活动中获取详细信息,然后我尝试在列表视图中显示详细信息。请任何人帮忙。
package com.appzoy.bluetoothlowenergyapplication;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class Alert_Dialog extends Activity {
Button pair,cancel;
ListView devicelist;
ArrayAdapter<String> arrayadapter;
ArrayList<String> list;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alertdialog_file);
pair=(Button) findViewById(R.id.btnPair);
cancel=(Button) findViewById(R.id.btnCancel);
devicelist=(ListView) findViewById(R.id.devicelistview);
list=new ArrayList<String>();
arrayadapter= new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
devicelist.setAdapter(arrayadapter);
pair.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String data = getIntent().getExtras().getString("devicename");
list.add(data);
arrayadapter.notifyDataSetChanged();
}
});
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}