嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元微信扫码支付:2 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
package com.example.weichen.afinal;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private Button btn, btn2;
private TextView textView;
private ListView scanlist;
private ArrayList<String> deviceName; //用來裝搜尋到的設備名稱
private ListAdapter listAdapter; //等等用來將listView綁上Adapter
private BluetoothManager bluetoothManager;
private BluetoothAdapter mBluetoothAdapter;
private static final String TAG = "mylogggg";
private ArrayList<BluetoothDevice> mBluetoothDevices = new ArrayList<BluetoothDevice>();
ArrayAdapter<String> btArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn_find); //按鈕打開藍芽
btn2 = (Button) findViewById(R.id.button2); //按鈕偵測
textView = (TextView) findViewById(R.id.textView);
scanlist = (ListView) findViewById(R.id.mListView);
btn.setOnClickListener(mybtn1);
btn2.setOnClickListener(mybtn2);
//ListView相關宣告
deviceName = new ArrayList<String>();
listAdapter = new ArrayAdapter<String>(getBaseContext(),
android.R.layout.simple_expandable_list_item_1, deviceName);//ListView使用的Adapter,
//listAdapter一定要先new出來,才能做setAdapter ////將listView綁上Adapter
scanlist.setAdapter(listAdapter);
System.out.println(deviceName);
}
//『是否開啟藍芽』 (沒問題)
private Button.OnClickListener mybtn1 = new Button.OnClickListener() {
@Override
public void onClick(View v) {
//得到 BluetoothAdapter對象
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
//判斷 BluetoothAdapter 對象是否為空,若空則表示本機無藍芽設備
if (adapter == null) {
Toast toast = Toast.makeText(MainActivity.this, "本機無藍芽設備", Toast.LENGTH_SHORT);
toast.show();
}
/*if (!adapter.isEnabled()) {
//用Intent提示使用者開啟藍芽 (用Intent從APP本身要求開啟)
Intent in = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(in);
Toast toast = Toast.makeText(MainActivity.this, "已經開藍芽了", Toast.LENGTH_SHORT);
toast.show();
}*/
/* 下面三行 為了讓mBluetoothAdapter.enable()不會crash ,所以需要*/
bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
mBluetoothAdapter = bluetoothManager.getAdapter();
}
if (mBluetoothAdapter.enable()) { //(從系統本身要求開啟)
Toast toast = Toast.makeText(MainActivity.this, "已經開啟藍芽了", Toast.LENGTH_SHORT);
toast.show();
}
}
};
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
//使用runOnUiThread方法,其功能等同於WorkThread透過Handler將資訊傳到MainThread(UiThread)中,
//詳細可進到runOnUiThread中觀察
@Override
public void run() {
if (!mBluetoothDevices.contains(device)) { //利用contains判斷是否有搜尋到重複的device
mBluetoothDevices.add(device); //如沒重複則添加到bluetoothdevices中
//將device的Name、rssi、address裝到此ArrayList<Strin>中
deviceName.add(device.getName() " rssi:" rssi "\r\n" device.getAddress());
//使用notifyDataSetChanger()更新listAdapter的內容
((BaseAdapter) listAdapter).notifyDataSetChanged();
}
}
});
}
};
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
btArrayAdapter.add("Name: " device.getName() "\n" "Address: " device.getAddress()
"\n" "rssi: " rssi " dBm");
btArrayAdapter.notifyDataSetChanged();
}
}
};
private Button.OnClickListener mybtn2 = new Button.OnClickListener(){
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
mBluetoothAdapter.startLeScan(mLeScanCallback);//跳到mLeScanCallback開始搜尋BLE設備
}
textView.setText("Scanning");
Log.d(TAG, "Start Scan");
System.out.println(deviceName);
}
};
}