我需要确定我的android手机屏幕是开着还是关着
我需要确定我的android手机屏幕是开着还是关着。在确定它是开还是关后,我需要向我的arduino发送一条蓝牙信息,并与之配对。1=开,0=关。我需要确定我的android手机屏幕是开着还是关着,android,arduino,Android,Arduino,我需要确定我的android手机屏幕是开着还是关着。在确定它是开还是关后,我需要向我的arduino发送一条蓝牙信息,并与之配对。1=开,0=关。 有人能帮我做这件事吗?我不知道什么是arduino,但这可能有助于检查屏幕是否关闭: Intent.ACTION\u SCREEN\u OFF和ACTION\u SCREEN\u ON检查上述广播注册。你可以找到一个很好的例子 要从android设备向arduino发送数据,您可以使用以下代码: private OutputStream output
有人能帮我做这件事吗?我不知道什么是
arduino
,但这可能有助于检查屏幕是否关闭:
Intent.ACTION\u SCREEN\u OFF和ACTION\u SCREEN\u ON检查上述广播注册。你可以找到一个很好的例子
要从android设备向arduino发送数据,您可以使用以下代码:
private OutputStream outputStream;
private InputStream inStream;
private void init() throws IOException {
BluetoothAdapter blueAdapter = BluetoothAdapter.getDefaultAdapter();
if (blueAdapter != null) {
if (blueAdapter.isEnabled()) {
Set<BluetoothDevice> bondedDevices = blueAdapter.getBondedDevices();
if(bondedDevices.size() > 0) {
Object[] devices = (Object []) bondedDevices.toArray();
BluetoothDevice device = (BluetoothDevice) devices[position];
ParcelUuid[] uuids = device.getUuids();
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuids[0].getUuid());
socket.connect();
outputStream = socket.getOutputStream();
inStream = socket.getInputStream();
}
Log.e("error", "No appropriate paired devices.");
} else {
Log.e("error", "Bluetooth is disabled.");
}
}
}
public void write(String s) throws IOException {
outputStream.write(s.getBytes());
}
public void run() {
final int BUFFER_SIZE = 1024;
byte[] buffer = new byte[BUFFER_SIZE];
int bytes = 0;
int b = BUFFER_SIZE;
while (true) {
try {
bytes = inStream.read(buffer, bytes, BUFFER_SIZE - bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private OutputStream OutputStream;
流内私有输入流;
私有void init()引发IOException{
BluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
如果(blueAdapter!=null){
if(blueAdapter.isEnabled()){
Set-bondedDevices=blueAdapter.getBondedDevices();
如果(bondedDevices.size()>0){
对象[]设备=(对象[])绑定设备。toArray();
BluetoothDevice设备=(BluetoothDevice)设备[位置];
parceluid[]uuids=device.getUuids();
BluetoothSocket=device.createrFComSocketToServiceRecord(uuids[0].getUuid());
socket.connect();
outputStream=socket.getOutputStream();
inStream=socket.getInputStream();
}
Log.e(“错误”,“没有合适的配对设备”);
}否则{
Log.e(“错误”,“蓝牙被禁用”);
}
}
}
public void write(字符串s)引发IOException{
write(s.getBytes());
}
公开募捐{
最终整数缓冲区大小=1024;
字节[]缓冲区=新字节[缓冲区大小];
int字节=0;
int b=缓冲区大小;
while(true){
试一试{
字节=流内读取(缓冲区,字节,缓冲区大小-字节);
}捕获(IOE异常){
e、 printStackTrace();
}
}
}