美文网首页
执行安卓中的蓝牙

执行安卓中的蓝牙

作者: 安安_660c | 来源:发表于2022-10-12 19:17 被阅读0次

    在许多方法中,蓝牙是一种在两个不同设备之间发送或接收数据的方法。Android 平台包括对蓝牙框架的支持,该框架允许设备与其他蓝牙设备无线交换数据。

    安卓提供蓝牙 API 来执行这些不同的操作。

    扫描其他蓝牙设备

    获取已配对设备的列表

    通过服务发现连接到其他设备

    安卓提供蓝牙适配器类与蓝牙通信。通过调用静态方法获取默认值适配器() 来创建此调用的对象。它的语法如下。

    private BluetoothAdapter BA;
    BA = BluetoothAdapter.getDefaultAdapter();
    

    为了启用设备的蓝牙,请使用以下蓝牙常量ACTION_REQUEST_ENABLE调用意图。它的语法是。

    Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(turnOn, 0);       
    
    除了这个常量之外,还有其他常量提供了API,它支持不同的任务。

    启用蓝牙后,您可以通过调用 getBonded设备() 方法获取配对设备的列表。它返回一组蓝牙设备。它的语法是。

    private Set<BluetoothDevice>pairedDevices;
    pairedDevices = BA.getBondedDevices();
    
    API中还有其他方法可以更好地控制蓝牙。

    此示例演示了蓝牙适配器类,用于操作蓝牙并通过蓝牙显示配对设备的列表。

    要试验此示例,您需要在实际设备上运行此示例。


    以下是 src/主要活动的内容.java
    package com.example.sairamkrishna.myapplication;
    
    import android.app.Activity;
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListView;
    
    import android.widget.Toast;
    import java.util.ArrayList;
    import java.util.Set;
    
    public class MainActivity extends Activity  {
       Button b1,b2,b3,b4;
       private BluetoothAdapter BA;
       private Set<BluetoothDevice>pairedDevices;
       ListView lv;
    
       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
    
          b1 = (Button) findViewById(R.id.button);
          b2=(Button)findViewById(R.id.button2);
          b3=(Button)findViewById(R.id.button3);
          b4=(Button)findViewById(R.id.button4);
    
          BA = BluetoothAdapter.getDefaultAdapter();
          lv = (ListView)findViewById(R.id.listView);
       }
    
       public void on(View v){
          if (!BA.isEnabled()) {
             Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
             startActivityForResult(turnOn, 0);
             Toast.makeText(getApplicationContext(), "Turned on",Toast.LENGTH_LONG).show();
          } else {
             Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_LONG).show();
          }
       }
    
       public void off(View v){
          BA.disable();
          Toast.makeText(getApplicationContext(), "Turned off" ,Toast.LENGTH_LONG).show();
       }
    
        
       public  void visible(View v){
          Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
          startActivityForResult(getVisible, 0);
       }
    
        
       public void list(View v){
          pairedDevices = BA.getBondedDevices();
            
          ArrayList list = new ArrayList();
    
          for(BluetoothDevice bt : pairedDevices) list.add(bt.getName());
          Toast.makeText(getApplicationContext(), "Showing Paired Devices",Toast.LENGTH_SHORT).show();
    
          final ArrayAdapter adapter = new  ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
            
          lv.setAdapter(adapter);
       }
    }
    

    以下是activity_main.xml

    这里abc表示关于教程点的标志。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent"
       android:layout_height="match_parent" 
       android:paddingLeft="@dimen/activity_horizontal_margin"
       android:paddingRight="@dimen/activity_horizontal_margin"
       android:paddingTop="@dimen/activity_vertical_margin"
       android:paddingBottom="@dimen/activity_vertical_margin"
       tools:context=".MainActivity"
       android:transitionGroup="true">
       
       <TextView android:text="Bluetooth Example" 
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/textview"
          android:textSize="35dp"
          android:layout_alignParentTop="true"
          android:layout_centerHorizontal="true" />
          
       <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Tutorials point"
          android:id="@+id/textView"
          android:layout_below="@+id/textview"
          android:layout_centerHorizontal="true"
          android:textColor="#ff7aff24"
          android:textSize="35dp" />
          
       <ImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/imageView"
          android:src="@drawable/abc"
          android:layout_below="@+id/textView"
          android:layout_centerHorizontal="true"
          android:theme="@style/Base.TextAppearance.AppCompat" />
          
       <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Turn On"
          android:id="@+id/button"
          android:layout_below="@+id/imageView"
          android:layout_toStartOf="@+id/imageView"
          android:layout_toLeftOf="@+id/imageView"
          android:clickable="true"
          android:onClick="on" />
          
       <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Get visible"
          android:onClick="visible"
          android:id="@+id/button2"
          android:layout_alignBottom="@+id/button"
          android:layout_centerHorizontal="true" />
          
       <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="List devices"
          android:onClick="list"
          android:id="@+id/button3"
          android:layout_below="@+id/imageView"
          android:layout_toRightOf="@+id/imageView"
          android:layout_toEndOf="@+id/imageView" />
          
       <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="turn off"
          android:onClick="off"
          android:id="@+id/button4"
          android:layout_below="@+id/button"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true" />
          
       <ListView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/listView"
          android:layout_alignParentBottom="true"
          android:layout_alignLeft="@+id/button"
          android:layout_alignStart="@+id/button"
          android:layout_below="@+id/textView2" />
          
       <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Paired devices:"
          android:id="@+id/textView2"
          android:textColor="#ff34ff06"
          android:textSize="25dp"
          android:layout_below="@+id/button4"
          android:layout_alignLeft="@+id/listView"
          android:layout_alignStart="@+id/listView" />
    
    </RelativeLayout>
    

    以下是字符串的内容.xml

    <resources>
       <string name="app_name">My Application</string>
    </resources>
    

    以下是安卓智能体的内容.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
       package="com.example.sairamkrishna.myapplication" >
       <uses-permission android:name="android.permission.BLUETOOTH"/>
       <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
       
       <application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" >
          
          <activity
             android:name=".MainActivity"
             android:label="@string/app_name" >
             
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
             
          </activity>
          
       </application>
    </manifest>
    

    让我们尝试运行您的应用程序。我假设您已经将实际的Android移动设备与计算机连接在一起。要从 Android Studio 运行应用,请打开项目的某个活动文件,然后单击工具栏中的“运行”图标。如果您的蓝牙无法打开,它将要求您允许启用蓝牙。

    相关文章

      网友评论

          本文标题:执行安卓中的蓝牙

          本文链接:https://www.haomeiwen.com/subject/puqxzrtx.html