Fragment4

本文介绍了一个简单的Android应用组件——Fragment4,该组件用于检测设备蓝牙的状态,并在TextView中显示蓝牙是否开启、关闭或未安装。代码实现了通过BluetoothAdapter获取默认适配器并判断蓝牙是否启用的功能。
package com.example.week;

import android.bluetooth.BluetoothAdapter;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
* Created by 生哥 on 2017/12/2.
*/

public class Fragment4 extends Fragment {
private TextView tv4;
private BluetoothAdapter bluetoothAdapter;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment4,container,false);
tv4 = view.findViewById(R.id.tv4);
return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null){
boolean b = bluetoothAdapter.isEnabled();
if (b){
tv4.setText("开启");
}else {
tv4.setText("关闭");
}
}else {
tv4.setText("没有");
}
}
}

转载于:https://www.cnblogs.com/wangwensheng/p/7975420.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值