安卓第六次作业——蓝牙通信

1. 在清单文件中申请蓝牙权限

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

    <!-- If your app targets Android 9 or lower, you can declare
         ACCESS_COARSE_LOCATION instead. -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <!-- ACCESS_COARSE_LOCATION:允许一个程序访问CellID或WiFi热点来获取大致的位置 -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

2. 设置蓝牙
2.1 获取全局的蓝牙适配器

所有蓝牙 Activity 都需要 BluetoothAdapter。如要获取 BluetoothAdapter,请调用静态的 getDefaultAdapter() 方法。此方法会返回一个 BluetoothAdapter 对象,表示设备自身的蓝牙适配器(蓝牙无线装置)。整个系统只有一个蓝牙适配器,并且您的应用可使用此对象与之进行交互。如果 getDefaultAdapter() 返回 null,则表示设备不支持蓝牙。
 

//在fragment的onCreateView中判断
if (bluetoothAdapter == null){
    text_tip.setText("当前设备不支持蓝牙功能!!!");
}
    //所有变量
	private final int REQUEST_ENABLE_BT = 0xa01;
    private final int ACCESS_LOCATION=1;

    private View view;
    private Button btn_open;
    private Button btn_linked;
    private Button btn_unLink;
    private TextView text_tip;
    //获取蓝牙适配器
    private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    private ListView listView;
    private SimpleAdapter listAdapter;
    private List<Map<String,Object>> listData;
    private Map<String,Object> listItem;

2.1 开启蓝牙

 通过startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);函数可以请求设备开启蓝牙。结果将通过onActivityResult中的resultCode回传。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_ENABLE_BT) {
        if (resultCode == RESULT_OK) {
            Toast.makeText(view.getContext(), "成功打开蓝牙", Toast.LENGTH_SHORT).show();
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(view.getContext(), "打开蓝牙失败", Toast.LENGTH_SHORT).show();
        }
    }
}
public void openBlueTooth(){
    if (!bluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
}

这样应用就可以开启设备蓝牙功能了。

3. 查找其他蓝牙设备

3.1 建立listView显示以查找到了蓝牙设备

添加bluetooth_list.xml用做list布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
    <TextView
        android:id="@+id/dv_name"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="@color/black"
        >
    </TextView>
    <TextView
        android:id="@+id/dv_mac"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        >

    </TextView>
</LinearLayout>

3.2 查询已配对设备


利用 BluetoothAdapter,您可以通过设备发现或查询配对设备的列表来查找远程蓝牙设备。

在执行设备发现之前,首先必须查询已配对的设备集,以了解所需的设备是否处于已检测到状态。为此,需要调用 getBondedDevices()。此方法会返回一组表示已配对设备的 BluetoothDevice 对象。
 

public void displayLinked() {
    Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
    if (pairedDevices.size() > 0) {
        for (BluetoothDevice device : pairedDevices) {
            String deviceName = device.getName();
            String deviceHardwareAddress = device.getAddress(); // MAC address
            listItem=new HashMap<String,Object>(); //必须在循环体里新建
            listItem.put("name", deviceName);
            listItem.put("mac",deviceHardwareAddress);
            listData.add(listItem);   //添加一条记录
        }
        listAdapter = new SimpleAdapter(view.getContext(),
                listData,
                R.layout.bluetooth_list, //自行创建的列表项布局
                new String[]{"name","mac"},
                new int[]{R.id.dv_name,R.id.dv_mac});
        listView.setAdapter(listAdapter);
    }else {
        Toast.makeText(view.getContext(), "没有配对的蓝牙设备", Toast.LENGTH_SHORT).show();
    }
}

3.3 搜索设备

如要开始发现设备,只需调用 startDiscovery()。该进程为异步操作,并且会返回一个布尔值,指示发现进程是否已成功启动。发现进程通常包含约 12 秒钟的查询扫描,随后会对发现的每台设备进行页面扫描,以检索其蓝牙名称。

但应用应用必须针对 ACTION_FOUND Intent 注册一个 BroadcastReceiver,以便接收每台发现的设备的相关信息。系统会为每台设备广播此 Intent。Intent 包含额外字段 EXTRA_DEVICE 和 EXTRA_CLASS,二者又分别包含 BluetoothDevice 和 BluetoothClass。
 

//在onCreateView()函数中启用管广播
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
view.getContext().registerReceiver(receiver, filter);

自己注册一个广播,并将每次搜索到的蓝牙设备添加到listView中。还需要在onDestroy()方法中关闭广播。

private final BroadcastReceiver receiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        System.out.println(BluetoothDevice.ACTION_FOUND.equals(action));
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            Toast.makeText(view.getContext(), "正在搜索....", Toast.LENGTH_SHORT).show();
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            String deviceName = device.getName();
            Log.i(TAG, "deviceName: "+deviceName);
            String deviceHardwareAddress = device.getAddress(); // MAC address
            listItem=new HashMap<String,Object>(); //必须在循环体里新建
            listItem.put("name", deviceName);
            listItem.put("mac",deviceHardwareAddress);
            listData.add(listItem);   //添加一条记录

            listAdapter = new SimpleAdapter(view.getContext(),
                    listData,
                    R.layout.bluetooth_list, //自行创建的列表项布局
                    new String[]{"name","mac"},
                    new int[]{R.id.dv_name,R.id.dv_mac});
            listView.setAdapter(listAdapter);
        }
    }
};

此外还需要一个申请搜索附近蓝牙的权限函数。

private void getPermission() {
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
        int permissionCheck = 0;
        permissionCheck = view.getContext().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
        permissionCheck += view.getContext().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION);

        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            //未获得权限
            this.requestPermissions( // 请求授权
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_COARSE_LOCATION},
                    ACCESS_LOCATION);// 自定义常量,任意整型
        }
    }
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case ACCESS_LOCATION:
            if (hasAllPermissionGranted(grantResults)) {
                Log.i(TAG, "onRequestPermissionsResult: 用户允许权限");
            } else {
                Log.i(TAG, "onRequestPermissionsResult: 拒绝搜索设备权限");
            }
            break;
    }
}

private boolean hasAllPermissionGranted(int[] grantResults) {
    for (int grantResult : grantResults) {
        if (grantResult == PackageManager.PERMISSION_DENIED) {
            return false;
        }
    }
    return true;
}

4. 布局文件与点击事件函数

4.1 fragment布局文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".FragmentFriend">

    <!-- TODO: Update blank fragment layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:orientation="horizontal"
            >
            <TextView
                android:id="@+id/tip"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="提示信息"
                android:textAlignment="center"
                android:textSize="24sp"
                >
            </TextView>
        </LinearLayout>
        <!--顶部结束-->
        <!--主体开始-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:orientation="vertical"
                android:gravity="center"
                >

                <Button
                    android:id="@+id/open_bluetooth"
                    android:layout_width="250dp"
                    android:layout_height="wrap_content"
                    android:text="请求开启蓝牙"
                    android:textSize="24sp"
                    >
                </Button>
                <Button
                    android:id="@+id/find_linked"
                    android:layout_width="250dp"
                    android:layout_height="wrap_content"
                    android:text="显示已连接设备"
                    android:textSize="24sp"
                    >
                </Button>
                <Button
                    android:id="@+id/find_unlink"
                    android:layout_width="250dp"
                    android:layout_height="wrap_content"
                    android:text="搜索设备"
                    android:textSize="24sp"
                    >
                </Button>
                <ListView
                    android:gravity="center"
                    android:id="@+id/listView2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:padding="5dip" >
                </ListView>
            </LinearLayout>
        </LinearLayout>

    </LinearLayout>
</FrameLayout>

4.2 点击事件

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.open_bluetooth:
            openBlueTooth();
            break;
        case R.id.find_linked:
            displayLinked();
            break;
        case R.id.find_unlink:
            //findDevice();
            getPermission();
            if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
                bluetoothAdapter.startDiscovery();
            }
            //Toast.makeText(view.getContext(), "正在搜索", Toast.LENGTH_SHORT).show();
            break;
        default:break;
    }
}

5. 完整代码

package com.example.bluetooth;

import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;
import static android.content.ContentValues.TAG;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class FragmentFriend extends Fragment implements View.OnClickListener {

    private final int REQUEST_ENABLE_BT = 0xa01;
    private final int ACCESS_LOCATION=1;

    private View view;
    private Button btn_open;
    private Button btn_linked;
    private Button btn_unLink;
    private TextView text_tip;
    //获取蓝牙适配器
    private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    private ListView listView;
    private SimpleAdapter listAdapter;
    private List<Map<String,Object>> listData;
    private Map<String,Object> listItem;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_friend, container, false);
        inItParam();
        listData = new ArrayList<Map<String,Object>>();
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        view.getContext().registerReceiver(receiver, filter);

        if (bluetoothAdapter == null){
            text_tip.setText("当前设备不支持蓝牙功能!!!");
        }
        return view;
    }
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            System.out.println(BluetoothDevice.ACTION_FOUND.equals(action));
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                Toast.makeText(view.getContext(), "正在搜索....", Toast.LENGTH_SHORT).show();
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String deviceName = device.getName();
                Log.i(TAG, "deviceName: "+deviceName);
                String deviceHardwareAddress = device.getAddress(); // MAC address
                listItem=new HashMap<String,Object>(); //必须在循环体里新建
                listItem.put("name", deviceName);
                listItem.put("mac",deviceHardwareAddress);
                listData.add(listItem);   //添加一条记录

                listAdapter = new SimpleAdapter(view.getContext(),
                        listData,
                        R.layout.bluetooth_list, //自行创建的列表项布局
                        new String[]{"name","mac"},
                        new int[]{R.id.dv_name,R.id.dv_mac});
                listView.setAdapter(listAdapter);
            }
        }
    };

    public void inItParam(){
        btn_open=view.findViewById(R.id.open_bluetooth);
        btn_open.setOnClickListener(this);

        btn_linked=view.findViewById(R.id.find_linked);
        btn_linked.setOnClickListener(this);

        btn_unLink=view.findViewById(R.id.find_unlink);
        btn_unLink.setOnClickListener(this);

        listView = view.findViewById(R.id.listView2);

        text_tip=view.findViewById(R.id.tip);
    }

    public void openBlueTooth(){
        if (!bluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }
    }
    public void displayLinked() {
        Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                String deviceName = device.getName();
                String deviceHardwareAddress = device.getAddress(); // MAC address
                listItem=new HashMap<String,Object>(); //必须在循环体里新建
                listItem.put("name", deviceName);
                listItem.put("mac",deviceHardwareAddress);
                listData.add(listItem);   //添加一条记录
            }
            listAdapter = new SimpleAdapter(view.getContext(),
                    listData,
                    R.layout.bluetooth_list, //自行创建的列表项布局
                    new String[]{"name","mac"},
                    new int[]{R.id.dv_name,R.id.dv_mac});
            listView.setAdapter(listAdapter);
        }else {
            Toast.makeText(view.getContext(), "没有配对的蓝牙设备", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_ENABLE_BT) {
            if (resultCode == RESULT_OK) {
                Toast.makeText(view.getContext(), "成功打开蓝牙", Toast.LENGTH_SHORT).show();
            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(view.getContext(), "打开蓝牙失败", Toast.LENGTH_SHORT).show();
            }
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.open_bluetooth:
                openBlueTooth();
                break;
            case R.id.find_linked:
                displayLinked();
                break;
            case R.id.find_unlink:
                //findDevice();
                getPermission();
                if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
                    bluetoothAdapter.startDiscovery();
                }
                //Toast.makeText(view.getContext(), "正在搜索", Toast.LENGTH_SHORT).show();
                break;
            default:break;
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        // Don't forget to unregister the ACTION_FOUND receiver.
        view.getContext().unregisterReceiver(receiver);
    }
    private void getPermission() {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            int permissionCheck = 0;
            permissionCheck = view.getContext().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
            permissionCheck += view.getContext().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION);

            if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                //未获得权限
                this.requestPermissions( // 请求授权
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                                Manifest.permission.ACCESS_COARSE_LOCATION},
                        ACCESS_LOCATION);// 自定义常量,任意整型
            }
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case ACCESS_LOCATION:
                if (hasAllPermissionGranted(grantResults)) {
                    Log.i(TAG, "onRequestPermissionsResult: 用户允许权限");
                } else {
                    Log.i(TAG, "onRequestPermissionsResult: 拒绝搜索设备权限");
                }
                break;
        }
    }

    private boolean hasAllPermissionGranted(int[] grantResults) {
        for (int grantResult : grantResults) {
            if (grantResult == PackageManager.PERMISSION_DENIED) {
                return false;
            }
        }
        return true;
    }
}

6. 结果演示

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值