zissis
Joined: 29 Jul 2011, 11:09 Posts: 3
|
Re: ANDROID TRILIZA ME BLUETOOTH ΜΕΤΑΞΥ ΔΥΟ ΚΙΝΗΤΩΝ
package com.apiit.Andro_Blue;
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Set; import java.util.UUID;
import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothServerSocket; import android.bluetooth.BluetoothSocket; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener;
public class Andro_Blue extends Activity { BluetoothAdapter adapter; private static final int REQUEST_ENABLE_BT=1; private static final int DISCOVERY_REQUEST=2; Button on, scan, send, discover; private ArrayAdapter<String> mNewDevicesArrayAdapter; private ArrayAdapter<String> mPairedDevicesArrayAdapter; private BluetoothSocket mmSocket=null; private BluetoothDevice device=null; private static final UUID MY_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66"); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); adapter=BluetoothAdapter.getDefaultAdapter(); on=(Button)findViewById(R.id.on); scan=(Button)findViewById(R.id.Scan); send=(Button)findViewById(R.id.Send); discover=(Button)findViewById(R.id.Discoverable); mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name); mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
//Switching Bluetooth ON From Here IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); this.registerReceiver(mReceiver, filter); filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); this.registerReceiver(mReceiver, filter); final ListView pairedListView = (ListView) findViewById(R.id.paired_list); pairedListView.setAdapter(mPairedDevicesArrayAdapter); final Set<BluetoothDevice> pairedDevices = adapter.getBondedDevices(); //Click Listener For Switching Bluetooth On on.setOnClickListener(new OnClickListener(){
@Override public void onClick(View v) { // TODO Auto-generated method stub if(adapter==null) { Toast.makeText(getApplicationContext(), "Device Dosen;t Supports Bluetooth", Toast.LENGTH_SHORT).show(); finish(); }else if(!adapter.isEnabled()) { Intent enablebt=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enablebt, REQUEST_ENABLE_BT); }else Toast.makeText(getApplicationContext(), "Bluetooth Already On", Toast.LENGTH_SHORT).show(); }
});
//Till Here discover.setOnClickListener(new OnClickListener(){
@Override public void onClick(View v) { // TODO Auto-generated method stub startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE),DISCOVERY_REQUEST); }
}); //Scan For Device From Here (Click Listener) scan.setOnClickListener(new OnClickListener(){
@Override public void onClick(View v) { // TODO Auto-generated method stub if (pairedDevices.size() > 0) { findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE); for (BluetoothDevice device : pairedDevices) { mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } } else { String noDevices ="No Devices Paired"; mPairedDevicesArrayAdapter.add(noDevices); } pairedListView.setAdapter(mPairedDevicesArrayAdapter); pairedListView.setOnItemClickListener(mDeviceClickListener); if (!adapter.isDiscovering()) { Toast.makeText(getApplicationContext(), "devicename", Toast.LENGTH_LONG).show(); adapter.startDiscovery(); ListView newDevicesListView = (ListView) findViewById(R.id.list); newDevicesListView.setAdapter(mNewDevicesArrayAdapter); newDevicesListView.setOnItemClickListener(mDeviceClickListener); }
}
}); // Till Here }
//Receiver for found devices From Here private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { findViewById(R.id.title_found_devices).setVisibility(View.VISIBLE); BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // If it's already paired, skip it, because it's been listed already if (device.getBondState() != BluetoothDevice.BOND_BONDED) { mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } // When discovery is finished, change the Activity title } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { setProgressBarIndeterminateVisibility(false); setTitle("Select Device"); if (mNewDevicesArrayAdapter.getCount() == 0) { String noDevices = "No Devices"; mNewDevicesArrayAdapter.add(noDevices); } } } }; // Till Here private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) { // TODO Auto-generated method stub
String info = ((TextView) v).getText().toString(); String address = info.substring(info.length() - 17); device = adapter.getRemoteDevice(address); Toast.makeText(getApplicationContext(), arg3 + "cONNECTING", Toast.LENGTH_SHORT).show(); try {
mmSocket = device.createRfcommSocketToServiceRecord(MY_UUID); mmSocket.connect(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Toast.makeText(getApplicationContext(), address, Toast.LENGTH_SHORT).show(); sendMessage("koi"); String MSG=listenForMessage(); Toast.makeText(getApplicationContext(), MSG, Toast.LENGTH_SHORT).show(); }
};
private void sendMessage(String message){ Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); OutputStream outStream; try { outStream = mmSocket.getOutputStream(); // Add a stop character. byte[] byteArray = (message + " ").getBytes(); byteArray[byteArray.length - 1] = 0; outStream.write(byteArray);
} catch (IOException e) { } }
private String listenForMessage(){ Toast.makeText(getApplicationContext(), "inListen", Toast.LENGTH_SHORT).show(); String result = ""; String message=""; int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; try { InputStream instream = mmSocket.getInputStream(); int bytesRead = -1; while (true) { bytesRead = instream.read(buffer); if (bytesRead != -1) { while ((bytesRead == bufferSize) && (buffer[bufferSize-1] != 0)){ message = message + new String(buffer, 0, bytesRead); bytesRead = instream.read(buffer); } message = message + new String(buffer, 0, bytesRead - 1); Toast.makeText(getApplicationContext(), message + "2", Toast.LENGTH_SHORT).show(); return result; } } } catch (IOException e) {} return result;
}
protected void onActivityResult(int requestCode,int resultCode, Intent data) { if (requestCode == DISCOVERY_REQUEST) { Toast.makeText(getApplicationContext(), "In Discovery", Toast.LENGTH_SHORT).show(); boolean isDiscoverable = resultCode > 0; int discoverableDuration = resultCode; if (isDiscoverable) { UUID uuid = UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666"); String name = "bluetoothserver"; final BluetoothServerSocket btserver; try { btserver = adapter.listenUsingRfcommWithServiceRecord(name, uuid); Thread acceptThread = new Thread(new Runnable() { public void run() { try { BluetoothSocket serverSocket = btserver.accept(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); acceptThread.start(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }
}
} kai tou tic tac toe
/* * @(#)TicTacToe.java 1.12 06/02/22 * * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * -Redistribution of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * -Redistribution in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of contributors may * be used to endorse or promote products derived from this software without * specific prior written permission. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of any * nuclear facility. */
/* * @(#)TicTacToe.java 1.12 06/02/22 */
import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.net.*; import java.applet.*;
/** * A TicTacToe applet. A very simple, and mostly brain-dead * implementation of your favorite game! <p> * * In this game a position is represented by a white and black * bitmask. A bit is set if a position is ocupied. There are * 9 squares so there are 1<<9 possible positions for each * side. An array of 1<<9 booleans is created, it marks * all the winning positions. * * @version 1.2, 13 Oct 1995 * @author Arthur van Hoff * @modified 04/23/96 Jim Hagen : winning sounds * @modified 02/10/98 Mike McCloskey : added destroy() */ public class TicTacToe extends Applet implements MouseListener { /** * White's current position. The computer is white. */ int white;
/** * Black's current position. The user is black. */ int black;
/** * The squares in order of importance... */ final static int moves[] = {4, 0, 2, 6, 8, 1, 3, 5, 7};
/** * The winning positions. */ static boolean won[] = new boolean[1 << 9]; static final int DONE = (1 << 9) - 1; static final int OK = 0; static final int WIN = 1; static final int LOSE = 2; static final int STALEMATE = 3;
/** * Mark all positions with these bits set as winning. */ static void isWon(int pos) { for (int i = 0 ; i < DONE ; i++) { if ((i & pos) == pos) { won[i] = true; } } }
/** * Initialize all winning positions. */ static { isWon((1 << 0) | (1 << 1) | (1 << 2)); isWon((1 << 3) | (1 << 4) | (1 << 5)); isWon((1 << 6) | (1 << 7) | (1 << 8)); isWon((1 << 0) | (1 << 3) | (1 << 6)); isWon((1 << 1) | (1 << 4) | (1 << 7)); isWon((1 << 2) | (1 << 5) | (1 << 8)); isWon((1 << 0) | (1 << 4) | (1 << 8)); isWon((1 << 2) | (1 << 4) | (1 << 6)); }
/** * Compute the best move for white. * @return the square to take */ int bestMove(int white, int black) { int bestmove = -1;
loop: for (int i = 0 ; i < 9 ; i++) { int mw = moves[i]; if (((white & (1 << mw)) == 0) && ((black & (1 << mw)) == 0)) { int pw = white | (1 << mw); if (won[pw]) { // white wins, take it! return mw; } for (int mb = 0 ; mb < 9 ; mb++) { if (((pw & (1 << mb)) == 0) && ((black & (1 << mb)) == 0)) { int pb = black | (1 << mb); if (won[pb]) { // black wins, take another continue loop; } } } // Neither white nor black can win in one move, this will do. if (bestmove == -1) { bestmove = mw; } } } if (bestmove != -1) { return bestmove; }
// No move is totally satisfactory, try the first one that is open for (int i = 0 ; i < 9 ; i++) { int mw = moves[i]; if (((white & (1 << mw)) == 0) && ((black & (1 << mw)) == 0)) { return mw; } }
// No more moves return -1; }
/** * User move. * @return true if legal */ boolean yourMove(int m) { if ((m < 0) || (m > 8)) { return false; } if (((black | white) & (1 << m)) != 0) { return false; } black |= 1 << m; return true; }
/** * Computer move. * @return true if legal */ boolean myMove() { if ((black | white) == DONE) { return false; } int best = bestMove(white, black); white |= 1 << best; return true; }
/** * Figure what the status of the game is. */ int status() { if (won[white]) { return WIN; } if (won[black]) { return LOSE; } if ((black | white) == DONE) { return STALEMATE; } return OK; }
/** * Who goes first in the next game? */ boolean first = true;
/** * The image for white. */ Image notImage;
/** * The image for black. */ Image crossImage;
/** * Initialize the applet. Resize and load images. */ public void init() { notImage = getImage(getCodeBase(), "images/not.gif"); crossImage = getImage(getCodeBase(), "images/cross.gif");
addMouseListener(this); }
public void destroy() { removeMouseListener(this); }
/** * Paint it. */ public void paint(Graphics g) { Dimension d = getSize(); g.setColor(Color.black); int xoff = d.width / 3; int yoff = d.height / 3; g.drawLine(xoff, 0, xoff, d.height); g.drawLine(2*xoff, 0, 2*xoff, d.height); g.drawLine(0, yoff, d.width, yoff); g.drawLine(0, 2*yoff, d.width, 2*yoff);
int i = 0; for (int r = 0 ; r < 3 ; r++) { for (int c = 0 ; c < 3 ; c++, i++) { if ((white & (1 << i)) != 0) { g.drawImage(notImage, c*xoff + 1, r*yoff + 1, this); } else if ((black & (1 << i)) != 0) { g.drawImage(crossImage, c*xoff + 1, r*yoff + 1, this); } } } }
/** * The user has clicked in the applet. Figure out where * and see if a legal move is possible. If it is a legal * move, respond with a legal move (if possible). */ public void mouseReleased(MouseEvent e) { int x = e.getX(); int y = e.getY();
switch (status()) { case WIN: case LOSE: case STALEMATE: play(getCodeBase(), "audio/return.au"); white = black = 0; if (first) { white |= 1 << (int)(Math.random() * 9); } first = !first; repaint(); return; }
// Figure out the row/column Dimension d = getSize(); int c = (x * 3) / d.width; int r = (y * 3) / d.height; if (yourMove(c + r * 3)) { repaint();
switch (status()) { case WIN: play(getCodeBase(), "audio/yahoo1.au"); break; case LOSE: play(getCodeBase(), "audio/yahoo2.au"); break; case STALEMATE: break; default: if (myMove()) { repaint(); switch (status()) { case WIN: play(getCodeBase(), "audio/yahoo1.au"); break; case LOSE: play(getCodeBase(), "audio/yahoo2.au"); break; case STALEMATE: break; default: play(getCodeBase(), "audio/ding.au"); } } else { play(getCodeBase(), "audio/beep.au"); } } } else { play(getCodeBase(), "audio/beep.au"); } }
public void mousePressed(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public String getAppletInfo() { return "TicTacToe by Arthur van Hoff"; } }
|