Android Bluetooth(蓝牙)实例

来源:互联网 发布:windows loader 8.1 编辑:程序博客网 时间:2024/06/03 17:02

我用的是Android Studio

以下是 MainActivity.java 文件的内容:

package com.example.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Set;

/** 蓝牙常量说明
* 1、ACTION_REQUEST_DISCOVERABLE 此常数用于开启蓝牙的发现
* 2、ACTION_STATE_CHANGED 此常量将通知蓝牙状态已经改变
* 3、ACTION_FOUND 此常数用于接收关于所发现的每个设备的信息
*/

/** 方法及说明
* 1 enable() 这种方法使适配器,如果未启用
* 2 isEnabled() 如果适配器已启用此方法返回true
* 3 disable() 该方法禁用适配器
* 4 getName() 此方法返回的蓝牙适配器的名称
* 5 setName(String name) 此方法更改蓝牙名称
* 6 getState() 此方法返回蓝牙适配器的当前状态
* 7 startDiscovery() 此方法开始蓝牙120秒的发现过程。
*/

/** 步骤 描述
* 1 使用Android Studio创建Android应用程序,并将其命名为Bluetooth,
* 创建这个项目,确保目标SDK编译在Android SDK的最新版本或使用更高级别的API。
* 2 修改 src/MainActivity.java 文件中添加代码
* 3 如果修改所需的布局XML文件 res/layout/activity_main.xml 添加GUI组件
* 4 修改 res/values/string.xml 文件,并添加必要的字符串常量组件
* 5 修改 AndroidManifest.xml 添加必要的权限。
* 6 运行应用程序并选择运行Android的设备,并在其上安装的应用和验证结果。
*/
public class MainActivity extends AppCompatActivity {
//蓝牙适配器
private BluetoothAdapter mBluetoothAdapter;
//用来存放搜到的蓝牙
private Set<BluetoothDevice> mDevices;
private ListView mListView;
private ArrayList mList;
private ArrayAdapter mAdapter;
private TextView mConnectedView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//自定义方法初始化UI控件
initView();
initData();
}

private void initData() {
//实例化蓝牙适配器
mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();

}

private void initView() {
// mListView = (ListView) findViewById(R.id.lv_bluetooth_name);
// mConnectedView 指的是已被连接的(可用的蓝牙)
mConnectedView = (TextView) findViewById(R.id.tv_bluetooth_connected);


}

public void onClick(View view) {
if (view != null) {
switch (view.getId()){
/**
* 选择打开开启蓝牙。但是当选择它,蓝牙将不会被打开。
* 事实上它会询问许可,以启用蓝牙。
*/

case R.id.bt_bluetooth_on:
/**
* 判断BluetoothAdapter 是否已经在准备状态,没有的话,就打开
*/
if (!mBluetoothAdapter.isEnabled()) {
//调用下列蓝牙ACTION_REQUEST_ENABLE的意图
Intent turnOn =new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn,0);
Toast.makeText(MainActivity.this,
"turn on", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,
"Already on", Toast.LENGTH_LONG).show();
}
break;
/**
* 开启许可,允许其他蓝牙设备120秒内可以搜索到该设备
* 选择设置可见按钮来打开视图。
* 下面的屏幕会出现要求许可才能打开发现120秒
*/
case R.id.bt_bluetooth_visible:
if (mBluetoothAdapter.isEnabled()) {
Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible,0);
}else{
Toast.makeText(MainActivity.this, "请先开启蓝牙", Toast.LENGTH_SHORT).show();
}



break;
/**
* 选择列表中的设备选项。它会列出倒在列表视图中的配对设备。
* 就我而言,只有一个配对设备。它如下所示。
*/
case R.id.bt_bluetooth_list:

//可以通过调用 getBondedDevices()方法来获取配对设备列表
mDevices = mBluetoothAdapter.getBondedDevices();
//在这初始化ListView是为了方便刷新,显示数据
mListView = (ListView) findViewById(R.id.lv_bluetooth_name);
mList = new ArrayList();
if (mBluetoothAdapter.isEnabled()) {
for (BluetoothDevice bd :mDevices){
mList.add(bd.getName());
}
if (mList.size() != 0){
mConnectedView.setVisibility(View.VISIBLE);
}
Toast.makeText(MainActivity.this,
"Showing Paired Devices", Toast.LENGTH_SHORT).show();
mAdapter = new ArrayAdapter(
this,android.R.layout.simple_list_item_1,mList);
mListView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}else{
Toast.makeText(MainActivity.this, "请先开启蓝牙", Toast.LENGTH_SHORT).show();
}


break;
/**
* 选择关闭按钮来关闭蓝牙。
* 当关掉蓝牙指示成功切换关闭蓝牙会出现以下消息
*/
case R.id.bt_bluetooth_off:
//判断蓝牙是否关闭
if (mBluetoothAdapter.isEnabled()){
//未关闭
mBluetoothAdapter.disable();
mList.clear();
mConnectedView.setVisibility(View.INVISIBLE);
mAdapter.notifyDataSetChanged();

}

Toast.makeText(getApplicationContext(),"蓝牙已关闭" ,
Toast.LENGTH_LONG).show();

break;
default:
break;
}
}

}

// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// getMenuInflater().inflate(R.menu.main, menu);
// return true;
// }
}


下面是 activity_main.xml 文件的内容:

<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ScrollView
android:id="@+id/scv_bluetooth_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_bluetooth_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<Button
android:id="@+id/bt_bluetooth_on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="打开手机蓝牙"/>
<Button
android:id="@+id/bt_bluetooth_visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="允许检测性"/>
<Button
android:id="@+id/bt_bluetooth_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="打开已配对列表"/>
<Button
android:id="@+id/bt_bluetooth_off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="关闭蓝牙"/>
<TextView
android:visibility="invisible"
android:id="@+id/tv_bluetooth_connected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已配对设备"/>
<ListView
android:id="@+id/lv_bluetooth_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:visibility="invisible"
android:id="@+id/ll_bluetooth_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_bluetooth_usable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可用设备"/>
<TextView
android:id="@+id/tv_bluetooth_touch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="触摸可配对"/>
</LinearLayout>

</LinearLayout>
</RelativeLayout>


下面是 AndroidManifest.xml 文件中要添加的权限:

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>


界面展示:


点击“打开蓝牙”按钮:

点击“可检测性”按钮:


点击“打开已配对列表”按钮:


点击“关闭蓝牙”按钮:


1 0
原创粉丝点击