如何在安卓设备中启用蓝牙?
本例演示了如何在安卓设备中启用蓝牙。
步骤 1 − 在 Android Studio 中新建一个项目,转到 File ⇒ New Project,然后填写所有必需的详细信息来新建一个项目。
步骤 2 − 添加以下代码到 res/layout/activity_main.xml 中。
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:app = "http://schemas.android.com/apk/res-auto"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:gravity = "center"
android:layout_height = "match_parent"
tools:context = ".MainActivity">
<TextView
android:id = "@+id/text"
android:textSize = "30sp"
android:layout_width = "match_parent"
android:layout_height = "match_parent" />
</LinearLayout>在以上代码中,我们使用了一个文本视图来显示蓝牙状态。
步骤 3 − 添加以下代码到 src/MainActivity.java 中
package com.example.myapplication;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.os.Build;
import android.os.Bundle;
import android.os.health.SystemHealthManager;
import android.provider.Telephony;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
textView.setText("enable");
final BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
manager.getAdapter().enable();
}
});
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
}
}让我们尝试运行您的应用程序。我假设您已将您的实际安卓移动设备连接到您的计算机。要通过安卓工作室运行该应用程序,请打开您的一个项目活动文件,然后单击工具栏中的运行
图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示默认屏幕 –

点击此处 下载项目代码
广告
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP