Note that there are some explanatory texts on larger screens.

plurals
  1. POBluetooth in Android
    text
    copied!<p>I'm currently developing a Sofware using bluetooth to communicate.</p> <p>Here is my bluetooth class:</p> <pre><code>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 &lt; 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; } } </code></pre> <p>Now the problem is that I'm catching a "nullPointerException" when I call startActivityForResult in line 16.</p> <p>Where is my fault?</p> <p>Greets Henrik</p> <p>EDIT: the new code:</p> <pre><code>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 &lt; 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 &lt; 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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload