android_mars老师_蓝牙学习

这篇具有很好参考价值的文章主要介绍了android_mars老师_蓝牙学习。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

到了这里,关于android_mars老师_蓝牙学习的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android模拟蓝牙蓝牙键盘——适配Android和Windows

    学校寒假有个程序设计比赛,我也一直想要去写一个安卓模拟的蓝牙键盘,这样无论到哪里,比如班班通和没有键盘的电脑设备,有手机就可以操作它,也比USB方便一些。忙活了一个寒假,也走了不少歪路,终于整成了,下面分享一些经验。 (学校的软件设计比赛已经交了终

    2024年04月28日
    浏览(28)
  • Android 9.0 蓝牙功能之一:蓝牙设置

    本章节记录如何构建蓝牙设置。 注意蓝牙应用必须是 System App。 LocalBluetoothManager 是操作蓝牙的主要入口。 1.通过 LocalBluetoothManager,可以获取到LocalBluetoothAdapter;CachedBluetoothDeviceManager;BluetoothEventManager、LocalBluetoothProfileManager。 2.通过 BluetoothEventManager.registerCallback 注册回调,

    2023年04月24日
    浏览(39)
  • 蓝牙通信 Android开发实现手机间通过蓝牙传输文件

    MainActivity.java 根据以上代码的结构和功能,我会将它们分为以下几部分: 权限请求和检查 requestPermissions() 方法 checkLocationPermission() 方法 onRequestPermissionsResult() 方法 初始化和设置 onCreate() 方法 onStart() 方法 onActivityResult() 方法 蓝牙设备搜索和配对 discoverDevices() 方法 与列表交互

    2024年03月27日
    浏览(38)
  • Android 蓝牙权限(更新到 Android 12)

    https://developer.android.com/guide/topics/connectivity/bluetooth/permissions BLUETOOTH :访问蓝牙适配器的权限,用于执行蓝牙操作。 BLUETOOTH_ADMIN :管理蓝牙适配器的权限,包括启用/禁用蓝牙、扫描设备和进行配对等操作。 ACCESS_FINE_LOCATION 或 ACCESS_COARSE_LOCATION :访问设备位置的权限。在 And

    2024年02月16日
    浏览(28)
  • Android 蓝牙使用

    原文地址: Android 蓝牙使用 - Stars-One的杂货小窝 公司项目需求需要实现监听蓝牙耳机连接,且要获取蓝牙耳机电量功能,翻了不少官方文档,记录下技术调研代码 注:本文没有研究蓝牙配对功能 Android12以后,申请蓝牙权限需要申请一组,如新增的几个权限,需要一起申请 参考: 蓝牙权

    2024年02月03日
    浏览(17)
  • Android 蓝牙状态的监听

    客户在使用我公司的Flutter插件时,要求有一个蓝牙与设备重连的功能,我用公司提供的Android SDK只能实现超出和进入蓝牙范围进行重连,但是无法在蓝牙打开进行重连,这不得不让我使用Android手写一个广播监听。 1. 添加权限 2. 创建一个类继承 BroadcastReceiver 3. 动态注册和注销

    2024年02月10日
    浏览(31)
  • Android 蓝牙开发(一)

    蓝牙(Bluetooth)是一种无线技术标准,能够在短距离内实现设备之间的数据交换和通信。蓝牙技术最初由瑞典爱立信公司于1994年开发,其名称源自丹麦国王哈拉尔·布吕特的译名“Harald Bluetooth”,他曾统一了斯堪的纳维亚半岛。 蓝牙技术是基于无线射频技术的,工作频率为

    2024年02月07日
    浏览(27)
  • Android中的蓝牙技术

    随着智能化生活的发展,手机成为人们生活的必需品,而蓝牙技术也随之应运而生。蓝牙技术作为现代移动设备与设备之间传输数据的一种主流方式,已经广泛应用于手表、耳机、车载系统等多种设备。在Android设备中,蓝牙技术也被大量使用,本篇文章将介绍Android中的蓝牙

    2024年02月07日
    浏览(30)
  • Android 蓝牙开发( 四 )

    上一篇文章给大家分享了Kotlin版的Android蓝牙的基础知识和基础用法,不过上一篇都是一些零散碎片化的程序,,这一篇给大家分享Android蓝牙开发实战项目Kotlin+Compose的初步使用 Android Compose 蓝牙开发 下图所示:MyBluetoothDemo为刚刚创建的Android空项目,我们现在清单文件中把我

    2024年02月10日
    浏览(33)
  • Android蓝牙BLE开发

    最近正在研究Android的蓝牙BLE开发学习,以下是自己做的个人总结 首先得说明什么是低功耗蓝牙BLE,BLE的全称为Bluetooth low energy(或称Blooth LE,BLE),从英文全称便可以知晓其是一种低功耗的蓝牙技术,是蓝牙技术联盟设计和销售的一种个人局域网技术,旨在用于医疗保健、运

    2023年04月09日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包