Android - 概述

Android - 蓝牙

在许多方面,蓝牙是一种在两个不同设备之间发送或接收数据的方式. Android平台包括对蓝牙框架的支持,允许设备与其他蓝牙设备无线交换数据.

Android提供蓝牙API来执行这些不同的操作.

  • 扫描其他蓝牙设备

  • 获取配对设备列表

  • 通过服务发现连接到其他设备

Android提供BluetoothAdapter类到与蓝牙通信.通过调用静态方法getDefaultAdapter()创建此调用的对象.其语法如下:

private BluetoothAdapter BA;
BA = BluetoothAdapter.getDefaultAdapter();

要启用设备的蓝牙功能,请使用以下蓝牙常量ACTION_REQUEST_ENABLE调用意图.它的语法是.

Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);

除了这个常量之外,还有其他常量提供API,它支持不同的任务.它们列在下面.

Sr.No常数& description
1

ACTION_REQUEST_DISCOVERABLE

此常量用于开启发现蓝牙

2

ACTION_STATE_CHANGED

此常量将通知蓝牙状态已更改

3

ACTION_FOUND

此常量用于接收有关每个已发现设备的信息

启用蓝牙后,您可以通过调用getBondedDevices()方法获取配对设备的列表.它返回一组蓝牙设备.它的语法是.

 
 private Set< BluetoothDevice> pairedDevices; 
 pairedDevices = BA.getBondedDevices();

除了招架设备之外,API中还有其他方法可以更好地控制蓝牙.它们列在下面.

Sr.No方法& description
1

enable()

此方法启用适配器(如果未启用)

2

isEnabled()

如果启用了适配器,则此方法返回true

3

disable()

此方法禁用适配器

4

getName( )

此方法返回蓝牙适配器的名称

5

setName(String name )

此方法更改蓝牙名称

6

getState()

此方法返回蓝牙适配器的当前状态.

7

startDiscovery()

此方法启动蓝牙的发现过程120秒.

示例

此示例演示了BluetoothAdapter类,用于操作蓝牙并通过蓝牙显示配对设备列表.

要试验这个例子,你需要在实际设备上运行它.

步骤描述
1您将使用Android工作室创建一个Android应用程序包com.e. xample.sairamkrishna.myapplication.
2修改src/MainActivity.java文件以添加代码
3修改布局XML文件res/layout/activity_main.xml如果需要,添加任何GUI组件.
4修改AndroidManifest.xml以添加必要的权限.
5运行应用程序并选择正在运行的Android设备并在其上安装应用程序并验证结果.

以下是 src/MainActivity.java 的内容

package com.example.sairamkrishna.myapplication;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;

public class MainActivity extends Activity  {
   Button b1,b2,b3,b4;
   private BluetoothAdapter BA;
   private Set<BluetoothDevice>pairedDevices;
   ListView lv;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      b1 = (Button) findViewById(R.id.button);
      b2=(Button)findViewById(R.id.button2);
      b3=(Button)findViewById(R.id.button3);
      b4=(Button)findViewById(R.id.button4);

      BA = BluetoothAdapter.getDefaultAdapter();
      lv = (ListView)findViewById(R.id.listView);
   }

   public void on(View v){
      if (!BA.isEnabled()) {
         Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
         startActivityForResult(turnOn, 0);
         Toast.makeText(getApplicationContext(), "Turned on",Toast.LENGTH_LONG).show();
      } else {
         Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_LONG).show();
      }
   }

   public void off(View v){
      BA.disable();
      Toast.makeText(getApplicationContext(), "Turned off" ,Toast.LENGTH_LONG).show();
   }

    
   public  void visible(View v){
      Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
      startActivityForResult(getVisible, 0);
   }

    
   public void list(View v){
      pairedDevices = BA.getBondedDevices();
        
      ArrayList list = new ArrayList();

      for(BluetoothDevice bt : pairedDevices) list.add(bt.getName());
      Toast.makeText(getApplicationContext(), "Showing Paired Devices",Toast.LENGTH_SHORT).show();

      final ArrayAdapter adapter = new  ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
        
      lv.setAdapter(adapter);
   }
}

以下是 activity_main.xml 的内容

这里abc表示it1352的标识.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" 
   android:layout_width="match_parent"
   android:layout_height="match_parent" 
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context=".MainActivity"
   android:transitionGroup="true">
   
   <TextView android:text="Bluetooth Example" 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/abc"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:theme="@style/Base.TextAppearance.AppCompat" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Turn On"
      android:id="@+id/button"
      android:layout_below="@+id/imageView"
      android:layout_toStartOf="@+id/imageView"
      android:layout_toLeftOf="@+id/imageView"
      android:clickable="true"
      android:onClick="on" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Get visible"
      android:onClick="visible"
      android:id="@+id/button2"
      android:layout_alignBottom="@+id/button"
      android:layout_centerHorizontal="true" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="List devices"
      android:onClick="list"
      android:id="@+id/button3"
      android:layout_below="@+id/imageView"
      android:layout_toRightOf="@+id/imageView"
      android:layout_toEndOf="@+id/imageView" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="turn off"
      android:onClick="off"
      android:id="@+id/button4"
      android:layout_below="@+id/button"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true" />
      
   <ListView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/listView"
      android:layout_alignParentBottom="true"
      android:layout_alignLeft="@+id/button"
      android:layout_alignStart="@+id/button"
      android:layout_below="@+id/textView2" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Paired devices:"
      android:id="@+id/textView2"
      android:textColor="#ff34ff06"
      android:textSize="25dp"
      android:layout_below="@+id/button4"
      android:layout_alignLeft="@+id/listView"
      android:layout_alignStart="@+id/listView" />

</RelativeLayout>

以下是 Strings.xml的内容

<resources>
   <string name="app_name">My Application</string>
</resources>

以下是 AndroidManifest.xml的内容

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapplication" >
   <uses-permission android:name="android.permission.BLUETOOTH"/>
   <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
   
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name=".MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
   </application>
</manifest>

让我们尝试运行您的应用程序.我假设您已将实际的Android移动设备与计算机相关联.要从Android工作室运行应用程序,请打开项目的一个活动文件,然后单击运行Eclipse Run Icon icon从工具栏开始.如果你的蓝牙没有打开,那么它会请求你的许可启用蓝牙.

Anroid蓝牙教程

现在只需选择"获取可见"按钮即可打开您的可见性.将出现以下屏幕,询问您是否允许开启发现120秒.

Anroid蓝牙教程

现在只需选择List Devices选项.它将列出列表视图中的配对设备.就我而言,我只有一个配对设备.它显示如下.

Anroid Bluetooth Tutorial

现在只需选择关闭按钮以关闭蓝牙.当您关闭蓝牙指示蓝牙成功关闭时会出现以下消息.

Anroid Bluetooth Tutorial