MainActivity
package com.example.bluetooth2;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
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.net.nsd.NsdManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button discoveryButton = null;
private Button scanButton = null;
private BluetoothReceiver bluetoothReceiver = null;
private BluetoothAdapter bluetoothAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
discoveryButton = (Button) findViewById(R.id.discoveryButtonId);
discoveryButton.setOnClickListener(new DiscoveryButtonListener());
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
bluetoothReceiver = new BluetoothReceiver();
//注册广播接收器
registerReceiver(bluetoothReceiver,intentFilter);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
scanButton = (Button) findViewById(R.id.scanButtonId);
System.out.println(scanButton);
scanButton.setOnClickListener(new ScanButtonListener());
}
private class BluetoothReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
//可以从收到的Intent对象中,将代表远程蓝牙适配器的对象取出
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getAddress());
}
}
}
//修改蓝牙设备可见性
private class DiscoveryButtonListener implements View.OnClickListener{
@SuppressLint("MissingPermission")
@Override
public void onClick(View v) {
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//蓝牙设备可见性时间应小于300s,否则直接被300s覆盖。---》虽设置成500s,实际上就是300s.
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,500);
startActivity(discoverableIntent);
}
}
private class ScanButtonListener implements View.OnClickListener{
@SuppressLint("MissingPermission")
@Override
public void onClick(View v) {
bluetoothAdapter.startDiscovery();
}
}
@Override
protected void onDestroy(){
unregisterReceiver(bluetoothReceiver);
super.onDestroy();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.bluetooth2">
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Bluetooth2"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置可见性"
android:id="@+id/discoveryButtonId"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scanButtonId"
android:text="扫描文件"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
文章来源地址https://www.toymoban.com/news/detail-521346.html
文章来源:https://www.toymoban.com/news/detail-521346.html
到了这里,关于android_mars老师_蓝牙学习的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!