capacitor添加安卓平台搜索插件
This commit is contained in:
parent
974cb5f4be
commit
566110eaea
|
@ -52,15 +52,15 @@
|
||||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
|
||||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||||
<!-- Geolocation API -->
|
<!-- Geolocation API -->
|
||||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
<!-- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />-->
|
||||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
<!-- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />-->
|
||||||
<uses-feature android:name="android.hardware.location.gps" />
|
<!-- <uses-feature android:name="android.hardware.location.gps" />-->
|
||||||
<!-- Network API -->
|
<!-- Network API -->
|
||||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
<!-- Navigator.getUserMedia -->
|
<!-- Navigator.getUserMedia -->
|
||||||
<!-- Video -->
|
<!-- Video -->
|
||||||
<uses-permission android:name="android.permission.CAMERA" />
|
<!-- <uses-permission android:name="android.permission.CAMERA" />-->
|
||||||
<!-- Audio -->
|
<!-- Audio -->
|
||||||
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
<!-- <uses-permission android:name="android.permission.RECORD_AUDIO" />-->
|
||||||
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
|
<!-- <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>-->
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|
|
@ -20,13 +20,13 @@ public class MainActivity extends BridgeActivity {
|
||||||
//去除title
|
//去除title
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
//去掉Activity上面的状态栏
|
//去掉Activity上面的状态栏
|
||||||
getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
|
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||||
WindowManager.LayoutParams. FLAG_FULLSCREEN);
|
|
||||||
|
|
||||||
// Initializes the Bridge
|
// Initializes the Bridge
|
||||||
this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
|
this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
|
||||||
// Additional plugins you've installed go here
|
// Additional plugins you've installed go here
|
||||||
// Ex: add(TotallyAwesomePlugin.class);
|
// Ex: add(TotallyAwesomePlugin.class);
|
||||||
|
add(SearchDevicePlugin.class);
|
||||||
}});
|
}});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,4 +46,16 @@ public class MainActivity extends BridgeActivity {
|
||||||
}
|
}
|
||||||
return super.onKeyDown(keyCode, event);
|
return super.onKeyDown(keyCode, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWindowFocusChanged(boolean hasFocus) {
|
||||||
|
super.onWindowFocusChanged(hasFocus);
|
||||||
|
// 隐藏底部导航栏
|
||||||
|
if (hasFocus) {
|
||||||
|
getWindow().getDecorView().setSystemUiVisibility(
|
||||||
|
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|
||||||
|
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|
||||||
|
| View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,163 @@
|
||||||
|
package com.cxview.media_palyer_client;
|
||||||
|
|
||||||
|
import com.getcapacitor.JSObject;
|
||||||
|
import com.getcapacitor.Plugin;
|
||||||
|
import com.getcapacitor.NativePlugin;
|
||||||
|
import com.getcapacitor.PluginCall;
|
||||||
|
import com.getcapacitor.PluginMethod;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.DatagramPacket;
|
||||||
|
import java.net.DatagramSocket;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.SocketException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
@NativePlugin()
|
||||||
|
public class SearchDevicePlugin extends Plugin {
|
||||||
|
private static final String TAG = "SearchDevicePlugin";
|
||||||
|
DatagramSocket udp_socket = null;
|
||||||
|
boolean exit_flag = false;
|
||||||
|
|
||||||
|
HashSet<String> ip_addresses = new HashSet<>();
|
||||||
|
|
||||||
|
public SearchDevicePlugin() {
|
||||||
|
super();
|
||||||
|
try {
|
||||||
|
udp_socket = new DatagramSocket();
|
||||||
|
new Thread(() -> {
|
||||||
|
final int kPacketSize = 4 + 4 + 4 + 1 + 33 + 33;
|
||||||
|
|
||||||
|
byte[] buf = new byte[1024 * 1024];
|
||||||
|
DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
||||||
|
try {
|
||||||
|
while (!exit_flag) {
|
||||||
|
udp_socket.receive(packet);
|
||||||
|
if (packet.getLength() > 79) {
|
||||||
|
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(packet.getData()));
|
||||||
|
int magic_flag = reversalEndian(dis.readInt());
|
||||||
|
if(magic_flag == 0xEA86EA86) {
|
||||||
|
int version = reversalEndian(dis.readInt());
|
||||||
|
int data_length = reversalEndian(dis.readInt());
|
||||||
|
if(data_length + kPacketSize == packet.getLength()) {
|
||||||
|
byte crc = dis.readByte();
|
||||||
|
dis.skipBytes(33 + 33);
|
||||||
|
byte[] json_data = new byte[packet.getLength() - kPacketSize];
|
||||||
|
int ret_size = dis.read(json_data);
|
||||||
|
if(ret_size == json_data.length) {
|
||||||
|
String json_string = new String(json_data, 0, ret_size);
|
||||||
|
if(getCheckAddAll(json_string) == crc) {
|
||||||
|
try {
|
||||||
|
JSONObject json = new JSONObject(json_string);
|
||||||
|
if(json.get("command").equals("_SearchDevice")) {
|
||||||
|
ip_addresses.add(packet.getAddress().getHostAddress());
|
||||||
|
}
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
} catch (SocketException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void finalize() throws Throwable {
|
||||||
|
super.finalize();
|
||||||
|
exit_flag = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PluginMethod()
|
||||||
|
public void searchDevice(PluginCall call) {
|
||||||
|
// More code here...
|
||||||
|
ip_addresses.clear();
|
||||||
|
|
||||||
|
JSObject obj = new JSObject();
|
||||||
|
try {
|
||||||
|
JSObject o = new JSObject();
|
||||||
|
o.put("has_exception", false);
|
||||||
|
o.put("flag", 0); // 0 请求 1响应
|
||||||
|
o.put("rpc_id", 0);
|
||||||
|
o.put("command", "_SearchDevice");
|
||||||
|
o.put("device_type", "RK3568");
|
||||||
|
|
||||||
|
String output_str = o.toString();
|
||||||
|
|
||||||
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||||
|
DataOutputStream dos = new DataOutputStream(bos);
|
||||||
|
dos.writeInt(reversalEndian(0xEA86EA86));
|
||||||
|
dos.writeInt(reversalEndian(0));
|
||||||
|
dos.writeInt(reversalEndian(output_str.length()));
|
||||||
|
dos.writeByte(getCheckAddAll(output_str));
|
||||||
|
byte[] zero = new byte[33];
|
||||||
|
dos.write(zero);
|
||||||
|
dos.write(zero);
|
||||||
|
dos.writeBytes(output_str);
|
||||||
|
byte[] data = bos.toByteArray();
|
||||||
|
try {
|
||||||
|
udp_socket.send(new DatagramPacket(data, data.length, InetAddress.getByName("255.255.255.255"), 60427));
|
||||||
|
udp_socket.send(new DatagramPacket(data, data.length, InetAddress.getByName("255.255.255.255"), 61427));
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000 * 5);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuffer buffer = new StringBuffer();
|
||||||
|
buffer.append("[");
|
||||||
|
for(String ip : ip_addresses) {
|
||||||
|
buffer.append("\"");
|
||||||
|
buffer.append(ip);
|
||||||
|
buffer.append("\",");
|
||||||
|
}
|
||||||
|
buffer.delete(buffer.length() - 1, buffer.length());
|
||||||
|
buffer.append("]");
|
||||||
|
obj.put("ip_list", buffer);
|
||||||
|
call.resolve(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
static byte getCheckAddAll(String str) {
|
||||||
|
byte calc_value = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < str.length(); ++i) {
|
||||||
|
calc_value += str.charAt(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return calc_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int reversalEndian(int i) {
|
||||||
|
byte[] b = new byte[4];
|
||||||
|
b[0] = (byte) (0xff & i);
|
||||||
|
b[1] = (byte) ((0xff00 & i) >> 8);
|
||||||
|
b[2] = (byte) ((0xff0000 & i) >> 16);
|
||||||
|
b[3] = (byte) ((0xff000000 & i) >> 24);
|
||||||
|
return b[3] & 0xFF | (b[2] & 0xFF) << 8 | (b[1] & 0xFF) << 16
|
||||||
|
| (b[0] & 0xFF) << 24;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "media_player_client",
|
"name": "media_player_client",
|
||||||
"version": "1.4.2",
|
"version": "1.5.2",
|
||||||
"description": "A Quasar Framework app",
|
"description": "A Quasar Framework app",
|
||||||
"author": "fangxiang <fangxiang@cloudview.work>",
|
"author": "fangxiang <fangxiang@cloudview.work>",
|
||||||
"private": true,
|
"private": true,
|
||||||
|
|
Loading…
Reference in New Issue