package com.example.blue;
import java.nio.charset.Charset;
import java.util.List;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.le.AdvertiseCallback;
import android.bluetooth.le.AdvertiseData;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.BluetoothLeAdvertiser;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.os.Bundle;
import android.os.Handler;
import android.os.ParcelUuid;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class BluetoothMainActivity extends Activity implements OnClickListener {
private BluetoothLeAdvertiser advertiser;
private TextView mText;
private Button mDiscoverButton;
private Button mAdvertiseButton;
BluetoothLeScanner mBluetoothLeScanner;
Handler handler;
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.ff);
mText = (TextView) findViewById(R.id.text);
mDiscoverButton = (Button) findViewById(R.id.discover_btn);
mAdvertiseButton = (Button) findViewById(R.id.advertise_btn);
mDiscoverButton.setOnClickListener(this);
mAdvertiseButton.setOnClickListener(this);
handler = new Handler();
advertiser = BluetoothAdapter.getDefaultAdapter()
.getBluetoothLeAdvertiser();
mBluetoothLeScanner = BluetoothAdapter.getDefaultAdapter()
.getBluetoothLeScanner();
if (!BluetoothAdapter.getDefaultAdapter()
.isMultipleAdvertisementSupported()) {
Toast.makeText(this, "Multiple advertisement not supported",
Toast.LENGTH_SHORT).show();
mAdvertiseButton.setEnabled(false);
mDiscoverButton.setEnabled(false);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.discover_btn) {
discover();
} else if (v.getId() == R.id.advertise_btn) {
advertise();
}
}
private void discover() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
mBluetoothLeScanner.stopScan(mScanCallback);
}
}, 10 * 1000);
mBluetoothLeScanner.startScan(mScanCallback);
}
private void advertise() {
// TODO Auto-generated method stub
AdvertiseSettings settings = new AdvertiseSettings.Builder()
.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
.setConnectable(false).build();
ParcelUuid pUuid = new ParcelUuid(UUID.fromString(""));
AdvertiseData data = new AdvertiseData.Builder()
.setIncludeDeviceName(true)
.addServiceUuid(pUuid)
.addServiceData(pUuid,
"Data".getBytes(Charset.forName("UTF-8"))).build();
final AdvertiseCallback advertisingCallback = new AdvertiseCallback() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
}
@Override
public void onStartFailure(int errorCode) {
super.onStartFailure(errorCode);
}
};
advertiser.startAdvertising(settings, data, advertisingCallback);
}
private ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
if (result == null || result.getDevice() == null
|| TextUtils.isEmpty(result.getDevice().getName()))
return;
StringBuilder builder = new StringBuilder(result.getDevice()
.getName());
builder.append("/n").append(
new String(result.getScanRecord().getServiceData(
result.getScanRecord().getServiceUuids().get(0)),
Charset.forName("UTF-8")));
mText.setText(builder.toString());
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
}
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
}
};
}
安卓5.0蓝牙低功耗 代码
最新推荐文章于 2021-06-02 20:33:36 发布