scan的执行问题

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);
        }
 
    }
}


正在回答 回答被采纳积分+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>


  • id_btn_start_scan这个按钮,你怎么写两种监听呢,写一种即可。两种的话,反而会影响程序效果,把android:onClick="onclick"注释掉。代码中对应的onclick(View view)也注释掉。就在startButton.setOnClickListener(new View.OnClickListener() {中监听。你调通这个监听器,保证让它走到Log.d(TAG,"button click");
    2018-11-16 12:00:12
提问者 安卓界的阿凡达 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);
        }
 
    }
});


  • 1)“scan函数不执行”你得先确认有没有走到scan(true)这一句代码。 2)你的意思说首次点击按钮后,Log.d(TAG,"button click");消息输出了,并且startButton 也更新成"Stop Scan"了,但走scan(true)没有任何效果,Log.d(TAG,"scan");也没有输出,是这个意思吗?3)”放在不同的地方,出现不同的结果“你是用的同一个手机吗?也有可能是不同版本造成的结果。
    2018-11-15 19:09:10
  • 1. 没有执行到onclick的函数里scan(true)这个函数,我也不知道原因 2. 点击按钮以后,没有任何输出 3. 同一个手机
    2018-11-15 19:59:37
  • 都没有执行setOnClickListener中的Log,那就证明你监听器绑定的有问题。跟scan()没有关系。你再对应的看一下xml,是不是放错地方了。你得先保证它回调了onclick()
    2018-11-16 09:46:25
提问者 安卓界的阿凡达 2018-11-15 16:10:06

我把can这个函数如果放在按钮的点击事件里,点击按钮以后,scan函数不执行。

但是我把scan函数放在onCreate函数里,它会自动执行,这是哪里的问题

1
<br>


  • public void onclick(View view) 你确定执行到了吗?Log.d(TAG,"onClick ");走了没有?没看到你对它有绑定。排查问题时可以先看下走的if还是else呢。startButton.setText()有没有更改就知道走的哪里了。
    2018-11-15 16:47:40
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
Android数据通信开发与应用2018版
  • 参与学习           人
  • 提交作业       147    份
  • 解答问题       687    个

本专题介绍了Android开发核心组件:广播、服务及全局应用。教会你如何使用AIDL、Thread、Socket、蓝牙解决进程线程间通信问题。利用Glide等实现异步加载及NDK原生代码开发。

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码