scan的执行问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | 我把can这个函数如果放在按钮的点击事件里,点击按钮以后,scan函数不执行。 但是我把scan函数放在onCreate函数里,它会自动执行,这是哪里的问题 package com.fanxin.android.bluetoothapplication; import android.Manifest; import android.annotation.TargetApi; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothManager; import android.bluetooth.le.BluetoothLeScanner; import android.bluetooth.le.ScanCallback; import android.bluetooth.le.ScanResult; import android.bluetooth.le.ScanSettings; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Build; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity-app" ; //蓝牙适配器 private BluetoothAdapter bluetoothAdapter; private Toast toast; //开始扫描打按钮 private Button startButton; //用来控制状态的变量,默认为false private boolean mIsScanStart = false ; private BluetoothLeScanner leScanner; private ScanSettings scanSettings; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); requestPermissions(); startButton = (Button)findViewById(R.id.id_btn_start_scan); startButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "button click" ); if (!mIsScanStart){ //如果还没开始扫描,则开始扫描 startButton.setText( "Stop Scan" ); mIsScanStart = true ; scan( true ); } else { startButton.setText( "Start Scan" ); mIsScanStart = false ; scan( false ); } } }); Toast.makeText(MainActivity. this , "hello" ,Toast.LENGTH_SHORT).show(); toast = Toast.makeText(MainActivity. this , " " ,Toast.LENGTH_SHORT); final BluetoothManager bluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE); bluetoothAdapter = bluetoothManager.getAdapter(); Log.d(TAG, "开始检查手机" ); if (bluetoothAdapter != null ){ showToast( "手机支持蓝牙!" ); Log.d(TAG, "手机支持蓝牙" ); } else { finish(); } if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)){ showToast( "手机不支持蓝牙BLE功能" ); finish(); } else { showToast( "手机支持蓝牙BLE功能" ); Log.d(TAG, "手机支持蓝牙BLE功能" ); } Log.d(TAG, "检查手机结束" ); //从adapter获取scanner if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Log.d(TAG, "获取scanner" ); leScanner = bluetoothAdapter.getBluetoothLeScanner(); scanSettings = new ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) .setReportDelay( 1000 ).build(); } // startButton = (Button)findViewById(R.id.id_btn_start_scan); // startButton.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View v) { // Log.d(TAG,"button click"); // // if (!mIsScanStart){ // //如果还没开始扫描,则开始扫描 // startButton.setText("Stop Scan"); // mIsScanStart = true; // scan(true); // }else { // startButton.setText("Start Scan"); // mIsScanStart = false; // scan(false); // } // // } // }); scan( true ); } @TargetApi ( 23 ) private void scan( boolean enable){ Log.d(TAG, "scan" ); final ScanCallback scanCallback = new ScanCallback() { @Override public void onScanResult( int callbackType, ScanResult result) { super .onScanResult(callbackType, result); BluetoothDevice device = result.getDevice(); //打印出名字和mac地址 //Log.d(TAG,"打印设备名称和MAC地址"); Log.d(TAG, "name = " + device.getName() + ", address = " + device.getAddress()); } }; if (enable){ Log.d(TAG, "start Scan" ); leScanner.startScan(scanCallback); Log.d(TAG, "start Scan over" ); } else { leScanner.stopScan(scanCallback); } } @Override protected void onResume() { super .onResume(); if (bluetoothAdapter!= null &&bluetoothAdapter.isEnabled()){ //申请打开蓝牙功能 Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivity(enableIntent); } } private void showToast(String msg){ toast.setText(msg); toast.show(); } /** *动态申请权限 *@author Fan Xin <fanxin.hit@gmail.com> *@time */ private void requestPermissions() { if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(MainActivity. this , Manifest.permission.ACCESS_COARSE_LOCATION)){ //已经有权限,直接做操作 } else { //没有权限 if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity. this , Manifest.permission.ACCESS_COARSE_LOCATION)){ } else { //申请权限 ActivityCompat.requestPermissions(MainActivity. this , new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 0 ); } } } public void onclick(View view) { scan( true ); Log.d(TAG, "onClick " ); if (!mIsScanStart){ //如果还没开始扫描,则开始扫描 startButton.setText( "Stop Scan" ); mIsScanStart = true ; scan( true ); } else { startButton.setText( "Start Scan" ); mIsScanStart = false ; scan( false ); } } } |
0
收起
正在回答 回答被采纳积分+1
3回答
安卓界的阿凡达
2018-11-16 10:42:31
xml文件,看不出有问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto" android:orientation = "vertical" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > < TextView android:id = "@+id/id_tv_ble" android:text = "" android:textSize = "20sp" android:layout_width = "match_parent" android:layout_height = "wrap_content" /> < Button android:id = "@+id/id_btn_start_scan" android:text = "Start Scan" android:layout_alignParentBottom = "true" android:onClick = "onclick" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> < Button android:id = "@+id/id_btn_connect" android:layout_above = "@id/id_btn_start_scan" android:text = "Connect" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> </ RelativeLayout > |
安卓界的阿凡达
2018-11-15 18:32:47
这里不是绑定吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | startButton = (Button)findViewById(R.id.id_btn_start_scan); startButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "button click" ); if (!mIsScanStart){ //如果还没开始扫描,则开始扫描 startButton.setText( "Stop Scan" ); mIsScanStart = true ; scan( true ); } else { startButton.setText( "Start Scan" ); mIsScanStart = false ; scan( false ); } } }); |
Android数据通信开发与应用2018版
- 参与学习 人
- 提交作业 147 份
- 解答问题 687 个
本专题介绍了Android开发核心组件:广播、服务及全局应用。教会你如何使用AIDL、Thread、Socket、蓝牙解决进程线程间通信问题。利用Glide等实现异步加载及NDK原生代码开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧