如何配对蓝牙设备并与之连接?

时间:2014-04-01 10:19:38

标签: android

我想使用Eclipse配对蓝牙设备。但我真的不知道修复此代码。以下是代码:

package com.example.bluetoothtest;



import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import java.util.Set;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {


private static final int REQUEST_ENABLE_BT = 1;
private Button myonbutton;
private Button myoffbutton;
private Button mylistbutton;
private Button myfindbutton;
private TextView mytext;
private BluetoothAdapter myBluetoothAdapter;
private Set<BluetoothDevice> mypairedDevices;
private ListView myListView;
private ArrayAdapter<String> myArrayAdapter;


@Override

protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);




  myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  if(myBluetoothAdapter == null) {
      myonbutton.setEnabled(false);
      myoffbutton.setEnabled(false);
      mylistbutton.setEnabled(false);
      myfindbutton.setEnabled(false);
      mytext.setText("Status: Your device is not supported");


      Toast.makeText(getApplicationContext(),"Your device does not support Bluetooth",
             Toast.LENGTH_LONG).show();
  } else {
      mytext = (TextView) findViewById(R.id.text);
      myonbutton = (Button)findViewById(R.id.turnOn);
      myonbutton.setOnClickListener(new OnClickListener() {



        @Override

        public void onClick(View v) {

            // TODO Auto-generated method stub

            on(v);

        }

      });



      myoffbutton = (Button)findViewById(R.id.turnOff);

      myoffbutton.setOnClickListener(new OnClickListener() {



        @Override

        public void onClick(View v) {

            // TODO Auto-generated method stub

            off(v);

        }

      });



      mylistbutton = (Button)findViewById(R.id.paired);

      mylistbutton.setOnClickListener(new OnClickListener() {



        @Override

        public void onClick(View v) {

            // TODO Auto-generated method stub

            list(v);

        }

      });



      myfindbutton = (Button)findViewById(R.id.search);

      myfindbutton.setOnClickListener(new OnClickListener() {



        @Override

        public void onClick(View v) {

            // TODO Auto-generated method stub

            find(v);

        }

      });



      myListView = (ListView)findViewById(R.id.listView1);





      myArrayAdapter = new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1);

      myListView.setAdapter(myArrayAdapter);

    }

 }



    public void on(View view){

      if (!myBluetoothAdapter.isEnabled()) {

      Intent turnOnIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

     startActivityForResult(turnOnIntent, REQUEST_ENABLE_BT);



     Toast.makeText(getApplicationContext(),"Bluetooth turned on" ,

             Toast.LENGTH_LONG).show();

  }

  else{

     Toast.makeText(getApplicationContext(),"Bluetooth is already on",

             Toast.LENGTH_LONG).show();

         }

     }



   @Override

   protected void onActivityResult(int requestCode, int resultCode, Intent data) {

   // TODO Auto-generated method stub

   if(requestCode == REQUEST_ENABLE_BT){

       if(myBluetoothAdapter.isEnabled()) {

           mytext.setText("Status: Enabled");

       } else {  

           mytext.setText("Status: Disabled");

          }
      }
  }


   public void list(View view){



  mypairedDevices = myBluetoothAdapter.getBondedDevices();



  for(BluetoothDevice device : mypairedDevices)

      myArrayAdapter.add(device.getName()+ "\n" + device.getAddress());


  Toast.makeText(getApplicationContext(),"Show Paired Devices",

          Toast.LENGTH_SHORT).show();

  }


  final BroadcastReceiver bReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();



        if (BluetoothDevice.ACTION_FOUND.equals(action)) {



             BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);



             myArrayAdapter.add(device.getName() + "\n" + device.getAddress());

             myArrayAdapter.notifyDataSetChanged();

        }
    }
};


   public void find(View view) {

   if (myBluetoothAdapter.isDiscovering()) {



       myBluetoothAdapter.cancelDiscovery();

   }
   else {

        myArrayAdapter.clear();

        myBluetoothAdapter.startDiscovery();


        registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));   
      }   
  }

  public void off(View view){

  myBluetoothAdapter.disable();
  mytext.setText("Status: Disconnected");


  Toast.makeText(getApplicationContext(),"Bluetooth turned off",

          Toast.LENGTH_LONG).show();
  }


  @Override

  protected void onDestroy() {

   // TODO Auto-generated method stub

   super.onDestroy();
   unregisterReceiver(bReceiver);
     }


  }

1 个答案:

答案 0 :(得分:0)