Click here to Skip to main content
15,886,199 members

android connect to bluetooth

Janna Dela Cruz asked:

Open original thread
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>
Tags: Mobile Apps (Android)

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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