В настоящее время я разрабатываю программное обеспечение, используя Bluetooth для связи.
Вот мой класс Bluetooth:
public class btClass extends Activity implements View.OnClickListener
{
private BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
private static final int REQUEST_ENABLE_BT = 10;
private Context context;
private btReceiver mReceiver = new btReceiver();
private String[] result = new String[]{""};
private Activity hangar;
public btClass()
{
if (!this.adapter.isEnabled())
{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);
int result = 0;
onActivityResult(REQUEST_ENABLE_BT, result, enableBtIntent);
if(result != RESULT_OK)
{
String exception = "Bluetooth failure!\r\nEnabling bluetooth adapter failed!";
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this.context);
dlgAlert.setMessage(exception);
dlgAlert.setTitle("Hangar error");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
}
}
@Override
protected void onCreate(Bundle intent)
{
super.onCreate(intent);
this.hangar = this.getParent();
this.context = this.getApplicationContext();
}
public void onClick(View v)
{
doDiscovery();
}
private void doDiscovery()
{
if(this.adapter.isDiscovering())
this.adapter.cancelDiscovery();
if(!this.adapter.startDiscovery())
{
String exception = "Bluetooth failure!\r\nDiscovering bluetooth devices failed!";
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this.context);
dlgAlert.setMessage(exception);
dlgAlert.setTitle("Hangar error");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
try
{
this.registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestr
}
catch(Exception e)
{
String exception = "'registerReceiver' threw Exception of type '" + e.getMessage();
StackTraceElement[] trace = e.getStackTrace();
for(int i = 0; i < trace.length; i++)
exception = exception + "\r\n" + trace[i];
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage(exception);
dlgAlert.setTitle("Hangar error");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
while(this.adapter.isDiscovering())
;
unregisterReceiver(this.mReceiver);
result = new String[]{""};
if(this.mReceiver != null)
result = this.mReceiver.foundDevicesNames();
}
public String[] getBluetoothDevices()
{
return result;
}
}
Теперь проблема в том, что я улавливаю «nullPointerException», когда вызываю startActivityForResult в строке 16.
Где моя вина?
Greets
Хенрик
EDIT:
новый код:
package de.schweigstill.hangar;
import android.bluetooth.*;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.app.Activity;
import android.app.AlertDialog;
public class btClass extends Activity implements View.OnClickListener
{
private BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
private static final int REQUEST_ENABLE_BT = 10;
private Context context;
private btReceiver mReceiver = new btReceiver();
private String[] result = new String[]{""};
private HangarActivity hangar;
public btClass(Context context)
{
super();
}
@Override
public void onCreate(Bundle intent)
{
super.onCreate(intent);
this.hangar = (HangarActivity)this.getParent();
this.context = this.getApplicationContext();
}
public void onClick(View v)
{
if (!this.adapter.isEnabled())
{
Intent enableBtIntent = new intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);
int result = 0;
onActivityResult(REQUEST_ENABLE_BT, result, enableBtIntent);
if(result != RESULT_OK)
{
String exception = "Bluetooth failure!\r\nEnabling bluetooth adapter failed!";
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this.context);
dlgAlert.setMessage(exception);
dlgAlert.setTitle("Hangar error");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
}
doDiscovery();
}
private void doDiscovery()
{
try
{
if(this.adapter.isDiscovering())
this.adapter.cancelDiscovery();
if(!this.adapter.startDiscovery())
{
String exception = "Bluetooth failure!\r\nDiscovering bluetooth devices failed!";
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this.context);
dlgAlert.setMessage(exception);
dlgAlert.setTitle("Hangar error");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestr
while(this.adapter.isDiscovering())
;
unregisterReceiver(this.mReceiver);
result = new String[]{""};
if(this.mReceiver != null)
{
result = this.mReceiver.foundDevicesNames();
for(int i = 0; i < result.length; i++)
this.hangar.aAd.add(result[i]);
}
}
catch(Exception e)
{
String exception = "In 'doDiscovery()' has an Exception of type '" + e.toString() + "' been thrown \r\n Trace:";//.getMessage();
StackTraceElement[] trace = e.getStackTrace();
for(int i = 0; i < trace.length; i++)
exception = exception + "\r\n" + trace[i];
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage(exception);
dlgAlert.setTitle("Hangar error");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
}
public String[] getBluetoothDevices()
{
return result;
}
}