zoukankan      html  css  js  c++  java
  • [uiautomator篇] bluetooth---接口来做

    package com.softwinner.performance.frameratetest;
    
    import android.Manifest;
    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.content.pm.PackageManager;
    import android.os.Bundle;
    import android.support.v4.app.ActivityCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.widget.Toast;
    
    import java.util.Set;
    
    public class MainActivity extends AppCompatActivity {
    
        private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        private String mLogTag = "bluetoothtest";
        private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if(BluetoothDevice.ACTION_FOUND.equals(action)){
                    BluetoothDevice remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                        Log.i(mLogTag,remoteDevice.getName() + " address: " + remoteDevice.getAddress());
                }
                else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
                    Log.i(mLogTag,"search finished");
                }
    
            }
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.KILL_BACKGROUND_PROCESSES) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.KILL_BACKGROUND_PROCESSES},1);
            }
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
            }
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
            }
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},10);
            }
            searchDevices();
    
        }
    
        private void searchDevices(){
    
            if(mBluetoothAdapter == null){
                Log.i(mLogTag,"device not support bluetooth");
                Toast.makeText(MainActivity.this,"device not support bluetooth",Toast.LENGTH_SHORT).show();
                return;
            }
            if(!mBluetoothAdapter.isEnabled()){
                mBluetoothAdapter.enable();
                mBluetoothAdapter.cancelDiscovery();
                Toast.makeText(MainActivity.this,"bluetooth has open",Toast.LENGTH_SHORT).show();
            }
    
            IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
            filter.setPriority(Integer.MAX_VALUE);
            registerReceiver(mReceiver,filter);
    
            mBluetoothAdapter.startDiscovery();
            try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}
    //        showBoundDevices();
    
        }
        private void showBoundDevices(){
    //        List<Map<String, String>> mBoundDevicesList = new ArrayList<>();
            Set<BluetoothDevice> pairDevices = mBluetoothAdapter.getBondedDevices();
            if(pairDevices.size() > 0){
                for(BluetoothDevice boundDevice : pairDevices){
                    System.out.println(boundDevice.getName() + " " + boundDevice.getAddress());
                    Log.i(mLogTag, boundDevice.getName()+ " " + boundDevice.getAddress());
                }
            }
    
        }
    
    }

    主要的是

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.softwinner.performance.frameratetest">
        <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.BLUETOOTH"/>
        <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name="com.softwinner.performance.frameratetest.MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
  • 相关阅读:
    ddd
    对Map按key和value分别排序
    两端通信
    WinDBG调试.NET程序示例
    FAQ:仓储实现为什么在基础设施层?
    Please Send Me a Card
    Web API 入门指南
    Node.js
    聊天工具mychat
    C语言面试问答5
  • 原文地址:https://www.cnblogs.com/liuzhipenglove/p/7082309.html
Copyright © 2011-2022 走看看