Click here to Skip to main content
15,849,615 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,


I have this code i get in http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingAsAClient[^] to discover devices that is triggered by a button. But whenever i run it, the program always pop-up a message to force close it. i don't know why. please help me.. here's is my code.. Thanks!
C#
public void checkIfBluetoothOpen(View view) {

       BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
       if (mBluetoothAdapter == null) {
           // Device does not support Bluetooth
           AlertDialog.Builder builder=new AlertDialog.Builder(this);
           builder
           .setIcon(android.R.drawable.ic_dialog_alert)
           .setTitle("Sorry!")
           .setMessage("Your device does not support bluetooth.")
           .setPositiveButton("OK", null)
           .show();
       }
       else
       {
           if (!mBluetoothAdapter.isEnabled()) {

               // Bluetooth is close
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                   startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
           }

           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
                   mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
               }
           }
           // Create a BroadcastReceiver for ACTION_FOUND
           final BroadcastReceiver mReceiver = 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 address to an array adapter to show in a ListView
                       mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                   }
               }
           };
           // Register the BroadcastReceiver
           IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
           registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy

           Intent discoverableIntent = new
           Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
           discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
           startActivity(discoverableIntent);
       }
   }</bluetoothdevice>
Posted
Updated 20-Jan-13 1:57am
v2

1 solution

Generally an error like that is caused by inappropriate manifest file. Please check at the top of the document about setting permission.

XML
<manifest ... >
  <uses-permission android:name="android.permission.BLUETOOTH" />
  ...
</manifest>



If you have done that and still getting the error, it means that your emulator is not setup properly to use your System's bluetooth. You can just debug the App, copy the .apk in bin folder in your phone/tab and install it there. Test the app in real device.
 
Share this answer
 
v2
Comments
Janna Dela Cruz 24-Jan-13 19:54pm    
i already did that.. still getting the same..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900