код мэйн
Код: Выделить всё
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private boolean mScanning;
private Handler mHandler;
private static final long SCAN_PERIOD = 10000;
private int REQUEST_ENABLE_BT = 1;
private Button btn_scan;
private ListView list;
private BluetoothAdapter mBluetoothAdapter;
;
private String mBluetoothDeviceAddress;
private BluetoothGatt mBluetoothGatt;
private ArrayAdapter<BluetoothDevice> devices;
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
devices.add(device);
devices.notifyDataSetChanged();
}
});
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, "ble_not_supported", Toast.LENGTH_SHORT).show();
}
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
btn_scan = (Button) findViewById(R.id.button_scan);
list = (ListView) findViewById(R.id.list_device);
btn_scan.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
scanLeDevice(true);
//Toast.makeText("Scanning....",Toast.LENGTH_SHORT).show();
}
}
);
list.setAdapter(devices);
}
private void scanLeDevice(final boolean enable) {
if (enable) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
}
код активити
Код: Выделить всё
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="143dp"
android:layout_marginStart="146dp"
android:layout_marginTop="96dp"
android:text="button_scan"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@+id/list_device"
android:layout_width="395dp"
android:layout_height="499dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="152dp" />
</android.support.constraint.ConstraintLayout>