开始第一天,着手这个项目的学习。
首先写一个简单的布局文件,包含三个按钮,一个listView列表显示搜索到的蓝牙
样式如图:
布局文件如下:
<?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="vertical">
<Button
android:id="@+id/turn_on"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="turn on"
android:textAllCaps="false"/>
<Button
android:id="@+id/turn_off"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="turn off"
android:textAllCaps="false"/>
<Button
android:id="@+id/search_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="search"
android:textAllCaps="false"/>
<ListView
android:id="@+id/search_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
我将两个功能的代码暂写在MainActivity中,如下:
package com.example.hitmi.bluetoothtest;
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.