加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
log.txt 14.00 KB
一键复制 编辑 原始数据 按行查看 历史
Delete 提交于 2024-09-23 18:09 . no commit message
package com.po.easywatch;
import android.Manifest;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.app.ActivityCompat;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
@SuppressLint("MissingPermission")
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView mTextView;
private static final String uuid = "e82c3583-03eb-4e44-8c2e-97e9512f19dd";
private Handler mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
Log.i("zhaha", "handleMessage " + msg.what);
switch (msg.what) {
case 2001:
mTextView.append("\n" + msg.obj);
// sendEmptyMessageDelayed(2002, 200);
break;
case 2002:
mTextView.scrollBy(0, 200);
break;
case 3001:
//adb shell input tap x y
//swipe 200 500 400 500
postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(MainActivity.this, MyService.class);
i.putExtra("what", 3001);
startService(i);
}
}, 5000);
break;
case 2009:
break;
// case 2001:break;
// case 2001:break;
// case 2001:break;
// case 2001:break;
// case 2001:break;
// case 2001:break;
// case 2001:break;
// case 2001:break;
}
}
};
private boolean noPermission() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
Log.i("zhaha", "BLUETOOTH not granted ");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH}, 1001);
return true;
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADMIN) != PackageManager.PERMISSION_GRANTED) {
Log.i("zhaha", "BLUETOOTH_ADMIN not granted ");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_ADMIN}, 1001);
return true;
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.i("zhaha", "ACCESS_FINE_LOCATION not granted ");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1001);
return true;
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.i("zhaha", "ACCESS_COARSE_LOCATION not granted ");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1001);
return true;
}
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = findViewById(R.id.tv_info);
mTextView.setMovementMethod(ScrollingMovementMethod.getInstance());
findViewById(R.id.tv_title).setOnClickListener(this);
findViewById(R.id.btn1).setOnClickListener(this);
findViewById(R.id.btn2).setOnClickListener(this);
findViewById(R.id.btn3).setOnClickListener(this);
if (noPermission()) {
return;
}
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
Log.i("zhaha", "bluetoothAdapter.isEnabled()");
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothDevice.ACTION_UUID);
intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
intentFilter.addAction(BluetoothDevice.ACTION_ALIAS_CHANGED);
intentFilter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
intentFilter.addAction(BluetoothDevice.ACTION_NAME_CHANGED);
// registerReceiver(mr, intentFilter);
// bluetoothAdapter.startDiscovery();
// Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
// bondedDevices.stream().map(new Function<BluetoothDevice, String>() {
// @Override
// public String apply(BluetoothDevice bluetoothDevice) {
// String name = noEmpty(bluetoothDevice.getName());
// Log.i("zhaha", "getBondedDevices " + name + " / " + bluetoothDevice.getAddress());
// return name;
// }
// });
// watch
// E4:93:6A:D7:30:0E
// 0000110a-0000-1000-8000-00805f9b34fb,
// 0000111e-0000-1000-8000-00805f9b34fb,
// 00001115-0000-1000-8000-00805f9b34fb,
// 00001116-0000-1000-8000-00805f9b34fb,
// 0000110e-0000-1000-8000-00805f9b34fb,
// 00001112-0000-1000-8000-00805f9b34fb,
// 0000111f-0000-1000-8000-00805f9b34fb,
// 00000000-0000-1000-8000-00805f9b34fb,
// 00000000-0000-1000-8000-00805f9b34fb,
// a49ebb15-cb06-495c-9f4f-bb80a90cdf00,
// a49eaa15-cb06-495c-9f4f-bb80a90cdf00
// e82c3583-03eb-4e44-8c2e-97e9512f19dd,
// phone
// AC:D6:18:51:64:C4
// 00001105-0000-1000-8000-00805f9b34fb,
// 00001115-0000-1000-8000-00805f9b34fb,
// 00001116-0000-1000-8000-00805f9b34fb,
// 0000110e-0000-1000-8000-00805f9b34fb,
// 0000112f-0000-1000-8000-00805f9b34fb,
// 00001112-0000-1000-8000-00805f9b34fb,
// 0000111f-0000-1000-8000-00805f9b34fb,
// 00001132-0000-1000-8000-00805f9b34fb,
// 00df0ca9-80bb-4f9f-5c49-06cb15aa9ea4
// try {
// BluetoothDevice d = bluetoothAdapter.getRemoteDevice("E4:93:6A:D7:30:0E");
// Log.i("zhaha", "getRemoteDevice " + d.getName() + " / " + Arrays.toString(d.getUuids()));
//
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
// bluetoothAdapter.getBluetoothLeScanner().startScan(sc);
// new Handler().postDelayed(new Runnable() {
// @Override
// public void run() {
// bluetoothAdapter.getBluetoothLeScanner().stopScan(sc);
// }
// }, 3000);
}
}
private BluetoothSocket socket = null;
private InputStream mmInStream;
private OutputStream mmOutStream;
private void openServer() {
addInfo("openServer");
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
try {
BluetoothServerSocket serverSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord("aaa", UUID.fromString(uuid));
new Thread() {
@Override
public void run() {
while (true) {
try {
addInfo("wait to accept...");
socket = serverSocket.accept();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (socket != null) {
try {
addInfo("accept a socket!");
serverSocket.close();
mmInStream = socket.getInputStream();
inputThread.start();
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
super.run();
}
}.start();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.tv_title:
Toast.makeText(this, "click title", Toast.LENGTH_SHORT).show();
break;
case R.id.btn1:
openServer();
break;
case R.id.btn2:
if (!inputThread.isInterrupted()) {
inputThread.stop();
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
break;
case R.id.btn3:
// mHandler.sendEmptyMessageDelayed(3001, 5000);
mHandler.sendEmptyMessage(3001);
break;
}
}
private void addInfo(String msg) {
Message message = mHandler.obtainMessage(2001);
message.obj = msg;
mHandler.sendMessage(message);
}
private final Thread inputThread = new Thread() {
byte[] buffer = new byte[1024];
@Override
public void run() {
addInfo("inputThread run!");
while (true) {
try {
mmInStream.read(buffer);
mTextView.post(new Runnable() {
@Override
public void run() {
Date date = new Date();
String time = date.getHours() + ":" + date.getMinutes() + " > ";
addInfo(time + new String(buffer));
// mTextView.append(time + new String(buffer) + "\n");
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
};
private final ScanCallback sc = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.i("zhaha",
"onScanResult " + callbackType + " / " + noEmpty(result.getDevice().getName()) + " / " + result.getDevice().getAddress());
super.onScanResult(callbackType, result);
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
StringBuilder builder = new StringBuilder();
results.stream().map(new Function<ScanResult, String>() {
@Override
public String apply(ScanResult scanResult) {
builder.append(noEmpty(scanResult.getDevice().getName())).append(":>").append(scanResult.getDevice().getAddress())
.append(" , ");
return scanResult.toString();
}
});
Log.i("zhaha", "onBatchScanResults " + builder.toString());
super.onBatchScanResults(results);
}
@Override
public void onScanFailed(int errorCode) {
Log.i("zhaha", "onScanFailed " + errorCode);
super.onScanFailed(errorCode);
}
};
private final BroadcastReceiver mr = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device != null) {
Log.i("zhaha", "BroadcastReceiver " + action + " / " + noEmpty(device.getName()) + " / " + device.getAddress()
+ " / " + Arrays.toString(device.getUuids()));
}
// if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// String name = noEmpty(device.getName());
// Log.i("zhaha", "ACTION_FOUND " + name+ " / "+device.getAddress());
// }
}
};
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化