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

MJ_LEE

我想使用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);
     }


  }
碧玉

这里是蓝牙上的android开发人员

还有一些不错的示例:javacodegeeks的示例

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android蓝牙连接到配对的设备

来自分类Dev

脚本连接/断开配对的蓝牙设备

来自分类Dev

Android蓝牙连接到配对的设备

来自分类Dev

如何在Android上以编程方式配对和连接HID蓝牙设备(蓝牙键盘)

来自分类Dev

如何在Windows 8.1上重新建立与配对的蓝牙LE设备的连接

来自分类Dev

蓝牙设备已配对并已连接(无声音)

来自分类Dev

与远程低功耗蓝牙(BLE)设备配对时断开连接

来自分类Dev

在范围内时自动连接到配对的蓝牙设备

来自分类Dev

将Android应用重新连接到配对的蓝牙设备

来自分类Dev

蓝牙设备已配对并已连接(无声音)

来自分类Dev

将Android应用重新连接到配对的蓝牙设备

来自分类Dev

与远程低功耗蓝牙(BLE)设备配对时断开连接

来自分类Dev

配对的蓝牙设备无法连接,14.04桌面

来自分类Dev

使用Android Studio在蓝牙中连接配对的设备

来自分类Dev

一旦Windows 10 UWP中的连接断开,如何以编程方式连接到配对的蓝牙设备

来自分类Dev

如何通过iOS Web应用程序与蓝牙设备配对?

来自分类Dev

如何通过iOS Web应用程序与蓝牙设备配对?

来自分类Dev

如何从命令行取消蓝牙设备的配对

来自分类Dev

与蓝牙设备配对的用户权限

来自分类Dev

以编程方式配对蓝牙设备

来自分类Dev

与蓝牙设备配对的用户权限

来自分类Dev

Android使用线程无法在某些设备上建立与配对设备的蓝牙连接

来自分类Dev

蓝牙配对如何工作?

来自分类Dev

如何在Android中连接蓝牙设备

来自分类Dev

如何从Linux内核连接到蓝牙设备?

来自分类Dev

Android:如何断开已连接的蓝牙设备?

来自分类Dev

如何在Android中连接蓝牙设备

来自分类Dev

如何使用Raspbian保持与蓝牙设备的连接

来自分类Dev

如何计算通过蓝牙连接的设备数量?

Related 相关文章

  1. 1

    Android蓝牙连接到配对的设备

  2. 2

    脚本连接/断开配对的蓝牙设备

  3. 3

    Android蓝牙连接到配对的设备

  4. 4

    如何在Android上以编程方式配对和连接HID蓝牙设备(蓝牙键盘)

  5. 5

    如何在Windows 8.1上重新建立与配对的蓝牙LE设备的连接

  6. 6

    蓝牙设备已配对并已连接(无声音)

  7. 7

    与远程低功耗蓝牙(BLE)设备配对时断开连接

  8. 8

    在范围内时自动连接到配对的蓝牙设备

  9. 9

    将Android应用重新连接到配对的蓝牙设备

  10. 10

    蓝牙设备已配对并已连接(无声音)

  11. 11

    将Android应用重新连接到配对的蓝牙设备

  12. 12

    与远程低功耗蓝牙(BLE)设备配对时断开连接

  13. 13

    配对的蓝牙设备无法连接,14.04桌面

  14. 14

    使用Android Studio在蓝牙中连接配对的设备

  15. 15

    一旦Windows 10 UWP中的连接断开,如何以编程方式连接到配对的蓝牙设备

  16. 16

    如何通过iOS Web应用程序与蓝牙设备配对?

  17. 17

    如何通过iOS Web应用程序与蓝牙设备配对?

  18. 18

    如何从命令行取消蓝牙设备的配对

  19. 19

    与蓝牙设备配对的用户权限

  20. 20

    以编程方式配对蓝牙设备

  21. 21

    与蓝牙设备配对的用户权限

  22. 22

    Android使用线程无法在某些设备上建立与配对设备的蓝牙连接

  23. 23

    蓝牙配对如何工作?

  24. 24

    如何在Android中连接蓝牙设备

  25. 25

    如何从Linux内核连接到蓝牙设备?

  26. 26

    Android:如何断开已连接的蓝牙设备?

  27. 27

    如何在Android中连接蓝牙设备

  28. 28

    如何使用Raspbian保持与蓝牙设备的连接

  29. 29

    如何计算通过蓝牙连接的设备数量?

热门标签

归档