Guest User

Untitled

a guest
May 27th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.72 KB | None | 0 0
  1.  
  2. public class MainActivity extends AppCompatActivity {
  3.  
  4. private BluetoothAdapter BA;
  5. public final static String MODULE_MAC = "00:21:13:00:14:E4";
  6. private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
  7. private BluetoothSocket mmSocket;
  8. private BluetoothDevice mmDevice;
  9. public InputStream IStream;
  10. public OutputStream OStream;
  11. public FirebaseDatabase database = FirebaseDatabase.getInstance();
  12. public DatabaseReference myRef = database.getReference("RCCarNew/CurrentCommand");
  13. public DatabaseReference myRef2 = database.getReference("RCCarNew/ConnectStat");
  14. public DatabaseReference myRef3 = database.getReference("RCCarNew/FlashStatus");
  15. public boolean first1 = false;
  16. private static final int PERMISSION_REQ_ID = 22;
  17. private static final String[] REQUESTED_PERMISSIONS = {Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA};
  18. private RtcEngine mRtcEngine;
  19.  
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24.  
  25. BA = BluetoothAdapter.getDefaultAdapter();
  26.  
  27. checkSelfPermission(REQUESTED_PERMISSIONS[0], PERMISSION_REQ_ID);
  28. checkSelfPermission(REQUESTED_PERMISSIONS[1], PERMISSION_REQ_ID);
  29. initAgoraEngine();
  30. setupSession();
  31.  
  32.  
  33.  
  34. mRtcEngine.joinChannel(null, "test-channel", "Extra Optional Data", 0);
  35. mRtcEngine.setupLocalVideo(new VideoCanvas(findViewById(R.id.surfaceView2), VideoCanvas.RENDER_MODE_FIT, 0));
  36.  
  37.  
  38. Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  39. startActivityForResult(turnOn, 0);
  40. BluetoothDevice device = BA.getRemoteDevice(MODULE_MAC);
  41.  
  42. BluetoothSocket tmp = null;
  43. mmDevice = device;
  44.  
  45.  
  46. try {
  47. tmp = mmDevice.createInsecureRfcommSocketToServiceRecord(MY_UUID);
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. System.out.print("ERROR!! java.io.IOEXCEPTION");
  51. }
  52. mmSocket = tmp;
  53.  
  54. try {
  55. assert mmSocket != null;
  56. mmSocket.connect();
  57. IStream = mmSocket.getInputStream();
  58. OStream = mmSocket.getOutputStream();
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. System.out.print("ERROR!! java.io.IOEXCEPTION during connecting");
  62. }
  63.  
  64.  
  65. myRef.addValueEventListener(new ValueEventListener() {
  66. @Override
  67. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  68.  
  69. if (dataSnapshot.getValue() != null && first1){
  70.  
  71. try {
  72. OStream.write((dataSnapshot.getValue()).toString().getBytes());
  73. }catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76.  
  77.  
  78. }else{
  79. System.out.print("hmmn, send a mesage you big idiot");
  80. first1 = true;
  81.  
  82. }
  83. }
  84.  
  85. @Override
  86. public void onCancelled(@NonNull DatabaseError databaseError) {
  87.  
  88. }
  89. });
  90. myRef2.addValueEventListener(new ValueEventListener() {
  91. @Override
  92. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  93. BluetoothDevice device = BA.getRemoteDevice(MODULE_MAC);
  94.  
  95. BluetoothSocket tmp = null;
  96. mmDevice = device;
  97. if (dataSnapshot.getValue() == null){
  98. System.out.print("GIVE ME DATA TO PROCESS YOU IDIOT");
  99. }
  100. else{
  101. if (dataSnapshot.getValue() == "Y") {
  102.  
  103. try {
  104. tmp = mmDevice.createInsecureRfcommSocketToServiceRecord(MY_UUID);
  105. } catch (IOException e) {
  106. e.printStackTrace();
  107. System.out.print("ERROR!! java.io.IOEXCEPTION");
  108. }
  109. mmSocket = tmp;
  110.  
  111. try {
  112. assert mmSocket != null;
  113. mmSocket.connect();
  114. IStream = mmSocket.getInputStream();
  115. OStream = mmSocket.getOutputStream();
  116. } catch (IOException e) {
  117. e.printStackTrace();
  118. System.out.print("ERROR!! java.io.IOEXCEPTION during connecting");
  119. }
  120.  
  121. }
  122. }
  123. }
  124.  
  125. @Override
  126. public void onCancelled(@NonNull DatabaseError databaseError) {
  127.  
  128. }
  129. });
  130. myRef3.addValueEventListener(new ValueEventListener() {
  131. @Override
  132. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  133. if (dataSnapshot.getValue() == "O"){
  134. try {
  135. OStream.write("J".getBytes());
  136. } catch (IOException e) {
  137. e.printStackTrace();
  138. }
  139. }else{
  140. try {
  141. OStream.write("M".getBytes());
  142. } catch (IOException e) {
  143. e.printStackTrace();
  144. }
  145. }
  146. }
  147.  
  148. @Override
  149. public void onCancelled(@NonNull DatabaseError databaseError) {
  150.  
  151. }
  152. });
  153.  
  154.  
  155.  
  156.  
  157.  
  158. }
  159.  
  160.  
  161. public void checkSelfPermission(String permission, int requestCode) {
  162. if (ContextCompat.checkSelfPermission(this,
  163. permission)
  164. != PackageManager.PERMISSION_GRANTED) {
  165.  
  166. ActivityCompat.requestPermissions(this,
  167. REQUESTED_PERMISSIONS,
  168. requestCode);
  169. }
  170. }
  171.  
  172. @Override
  173. public void onRequestPermissionsResult(int requestCode,
  174. @NonNull String[] permissions, @NonNull int[] grantResults) {
  175. if (requestCode == PERMISSION_REQ_ID) {
  176. if (grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) {
  177. return;
  178. }
  179. // if permission granted, initialize the engine
  180. initAgoraEngine();
  181. }
  182. }
  183. private void initAgoraEngine() {
  184. try {
  185. mRtcEngine = RtcEngine.create(getBaseContext(), "944e894435fb4083a3cfc90066509245", mRtcEventHandler);
  186. } catch (Exception e) {
  187.  
  188. throw new RuntimeException("NEED TO check rtc sdk init fatal error\n" + Log.getStackTraceString(e));
  189. }
  190. setupSession();
  191. }
  192.  
  193. private void setupSession() {
  194. mRtcEngine.setChannelProfile(Constants.CHANNEL_PROFILE_COMMUNICATION);
  195.  
  196. mRtcEngine.enableVideo();
  197.  
  198. mRtcEngine.setVideoEncoderConfiguration(new VideoEncoderConfiguration(VideoEncoderConfiguration.VD_840x480, VideoEncoderConfiguration.FRAME_RATE.FRAME_RATE_FPS_30,
  199. VideoEncoderConfiguration.STANDARD_BITRATE,
  200. VideoEncoderConfiguration.ORIENTATION_MODE.ORIENTATION_MODE_FIXED_LANDSCAPE));
  201. }
  202.  
  203.  
  204. private final IRtcEngineEventHandler mRtcEventHandler = new IRtcEngineEventHandler() {
  205. @Override
  206. public void onFirstRemoteVideoFrame(int uid, int width, int height, int elapsed) {
  207. super.onFirstRemoteVideoFrame(uid, width, height, elapsed);
  208. mRtcEngine.setupRemoteVideo(new VideoCanvas(findViewById(R.id.surfaceView), VideoCanvas.RENDER_MODE_FIT, uid));
  209.  
  210. }
  211. };
  212.  
  213.  
  214.  
  215. }
Add Comment
Please, Sign In to add comment