Click here to Skip to main content
12,453,354 members (61,234 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Mobile Android
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!
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 19-Jan-13 17:26pm
Updated 20-Jan-13 0:57am
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

<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.
  Permalink  
v2
Comments
Janna Mamer 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100