❮ 上一节
下一节 ❯
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 还提供了其他常量,支持不同的任务。 它们在下面列出。
序号
常量 & 描述
1
ACTION_REQUEST_DISCOVERABLE
此常量用于开启蓝牙发现
2
ACTION_STATE_CHANGED
此常量将通知蓝牙状态已更改
3
ACTION_FOUND
此常量用于接收有关发现的每个设备的信息
启用蓝牙后,您可以通过调用 getBondedDevices() 方法获取已配对设备的列表。 它返回一组蓝牙设备。 它的语法是。
private Set
pairedDevices = BA.getBondedDevices();
除了配对的设备之外,API 中还有其他方法可以更好地控制蓝牙。 它们在下面列出。
序号
方法 & 描述
1
enable()
此方法在未启用时启用适配器
2
isEnabled()
如果启用了适配器,此方法返回 true
3
disable()
此方法禁用适配器
4
getName()
此方法返回蓝牙适配器的名称
5
setName(String name)
此方法更改蓝牙名称
6
getState()
此方法返回蓝牙适配器的当前状态。
7
startDiscovery()
此方法启动蓝牙的发现过程 120 秒。
示例
此示例提供了 BluetoothAdapter 类的演示,用于操作蓝牙并显示蓝牙配对设备列表。
要试验这个例子,你需要在实际设备上运行它。
步骤
描述
1
您将使用 Android Studio 创建一个 Android 应用程序包 com.example.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
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 表示关于 tutorialspoint 的标志。
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"> 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" /> 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" /> 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" />
这是 Strings.xml 的内容
这是 AndroidManifest.xml 的内容
package="com.example.sairamkrishna.myapplication" > android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > android:name=".MainActivity" android:label="@string/app_name" >
让我们尝试运行您的应用程序。 我假设您已将实际的 Android 移动设备与您的计算机连接起来。要从 Android Studio 运行应用程序,请打开项目的一个活动文件,然后单击工具栏中的运行 图标。如果你的蓝牙没有打开,它会询问你是否允许启用蓝牙。
现在只需选择 Get Visible 按钮即可打开您的可见性。 将出现以下屏幕,询问您是否允许打开发现 120 秒。
现在只需选择列出设备选项。 它将在列表视图中列出已配对的设备。 就我而言,我只有一个配对设备。 如下所示。
现在只需选择关闭按钮即可关闭蓝牙。 当您关闭蓝牙时,会出现以下消息,表明成功关闭蓝牙。
❮ 上一节
下一节 ❯